Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add line breakers to logs in VPN client. #595

Merged
merged 1 commit into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions cmd/apps/vpn-client/vpn-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,22 @@ func main() {

serverPK := cipher.PubKey{}
if err := serverPK.UnmarshalText([]byte(*serverPKStr)); err != nil {
fmt.Printf("Invalid VPN server pub key: %v", err)
fmt.Printf("Invalid VPN server pub key: %v\n", err)
os.Exit(1)
}

localPK := cipher.PubKey{}
if *localPKStr != "" {
if err := localPK.UnmarshalText([]byte(*localPKStr)); err != nil {
fmt.Printf("Invalid local PK: %v", err)
fmt.Printf("Invalid local PK: %v\n", err)
os.Exit(1)
}
}

localSK := cipher.SecKey{}
if *localSKStr != "" {
if err := localSK.UnmarshalText([]byte(*localSKStr)); err != nil {
fmt.Printf("Invalid local SK: %v", err)
fmt.Printf("Invalid local SK: %v\n", err)
os.Exit(1)
}
}
Expand All @@ -85,11 +85,11 @@ func main() {
parseIP := func(addr string) net.IP {
ip, ok, err := vpn.ParseIP(addr)
if err != nil {
fmt.Printf("Failed to parse IP %s: %v", addr, err)
fmt.Printf("Failed to parse IP %s: %v\n", addr, err)
return nil
}
if !ok {
fmt.Printf("Failed to parse IP %s", addr)
fmt.Printf("Failed to parse IP %s\n", addr)
return nil
}

Expand All @@ -111,7 +111,7 @@ func main() {
appClient := app.NewClient(eventSub)
defer appClient.Close()

fmt.Printf("Connecting to VPN server %s", serverPK.String())
fmt.Printf("Connecting to VPN server %s\n", serverPK.String())

appConn, err := dialServer(appClient, serverPK)
if err != nil {
Expand All @@ -120,18 +120,18 @@ func main() {
}
defer func() {
if err := appConn.Close(); err != nil {
fmt.Printf("Error closing connection to the VPN server: %v", err)
fmt.Printf("Error closing connection to the VPN server: %v\n", err)
}
}()

fmt.Printf("Dialed %s", appConn.RemoteAddr())
fmt.Printf("Dialed %s\n", appConn.RemoteAddr())

vpnClientCfg := vpn.ClientConfig{
Passcode: *passcode,
}
vpnClient, err := vpn.NewClient(vpnClientCfg, log, appConn)
if err != nil {
fmt.Printf("Error creating VPN client: %v", err)
fmt.Printf("Error creating VPN client: %v\n", err)
}

osSigs := make(chan os.Signal, 2)
Expand All @@ -150,7 +150,7 @@ func main() {
select {
case ip := <-directIPsCh:
if err := vpnClient.AddDirectRoute(ip); err != nil {
fmt.Printf("Failed to setup direct route to %s: %v", ip.String(), err)
fmt.Printf("Failed to setup direct route to %s: %v\n", ip.String(), err)
}
default:
directRoutesDone = true
Expand All @@ -160,21 +160,21 @@ func main() {
go func() {
for ip := range directIPsCh {
if err := vpnClient.AddDirectRoute(ip); err != nil {
fmt.Printf("Failed to setup direct route to %s: %v", ip.String(), err)
fmt.Printf("Failed to setup direct route to %s: %v\n", ip.String(), err)
}
}
}()

go func() {
for ip := range nonDirectIPsCh {
if err := vpnClient.RemoveDirectRoute(ip); err != nil {
fmt.Printf("Failed to remove direct route to %s: %v", ip.String(), err)
fmt.Printf("Failed to remove direct route to %s: %v\n", ip.String(), err)
}
}
}()

if err := vpnClient.Serve(); err != nil {
fmt.Printf("Error serving VPN: %v", err)
fmt.Printf("Error serving VPN: %v\n", err)
}
}

Expand Down
32 changes: 16 additions & 16 deletions internal/vpn/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewClient(cfg ClientConfig, l logrus.FieldLogger, conn net.Conn) (*Client,
return nil, fmt.Errorf("error getting default network gateway: %w", err)
}

fmt.Printf("Got default network gateway IP: %s", defaultGateway)
fmt.Printf("Got default network gateway IP: %s\n", defaultGateway)

return &Client{
cfg: cfg,
Expand Down Expand Up @@ -149,9 +149,9 @@ func (c *Client) Serve() error {
return fmt.Errorf("error during client/server handshake: %w", err)
}

fmt.Printf("Performed handshake with %s", c.conn.RemoteAddr())
fmt.Printf("Local TUN IP: %s", tunIP.String())
fmt.Printf("Local TUN gateway: %s", tunGateway.String())
fmt.Printf("Performed handshake with %s\n", c.conn.RemoteAddr())
fmt.Printf("Local TUN IP: %s\n", tunIP.String())
fmt.Printf("Local TUN gateway: %s\n", tunGateway.String())

suid, err := setupClientSysPrivileges()
if err != nil {
Expand All @@ -166,13 +166,13 @@ func (c *Client) Serve() error {
defer func() {
tunName := tun.Name()
if err := tun.Close(); err != nil {
fmt.Printf("Error closing TUN %s: %v", tunName, err)
fmt.Printf("Error closing TUN %s: %v\n", tunName, err)
}

c.releaseSysPrivileges(suid)
}()

fmt.Printf("Allocated TUN %s: %v", tun.Name(), err)
fmt.Printf("Allocated TUN %s: %v\n", tun.Name(), err)

if err := SetupTUN(tun.Name(), tunIP.String()+TUNNetmaskCIDR, tunGateway.String(), TUNMTU); err != nil {
return fmt.Errorf("error setting up TUN %s: %w", tun.Name(), err)
Expand All @@ -192,7 +192,7 @@ func (c *Client) Serve() error {
}

defer c.routeTrafficDirectly(tunGateway)
fmt.Printf("Routing all traffic through TUN %s: %v", tun.Name(), err)
fmt.Printf("Routing all traffic through TUN %s: %v\n", tun.Name(), err)
if err := c.routeTrafficThroughTUN(tunGateway); err != nil {
return fmt.Errorf("error routing traffic through TUN %s: %w", tun.Name(), err)
}
Expand All @@ -202,7 +202,7 @@ func (c *Client) Serve() error {
// this will be executed first on return, so we setup privileges once again,
// so other deferred clear up calls may be done successfully
if _, err := setupClientSysPrivileges(); err != nil {
fmt.Printf("Failed to setup system privileges to clear up: %v", err)
fmt.Printf("Failed to setup system privileges to clear up: %v\n", err)
}

connToTunDoneCh := make(chan struct{})
Expand All @@ -212,14 +212,14 @@ func (c *Client) Serve() error {
defer close(connToTunDoneCh)

if _, err := io.Copy(tun, c.conn); err != nil {
fmt.Printf("Error resending traffic from TUN %s to VPN server: %v", tun.Name(), err)
fmt.Printf("Error resending traffic from TUN %s to VPN server: %v\n", tun.Name(), err)
}
}()
go func() {
defer close(tunToConnCh)

if _, err := io.Copy(c.conn, tun); err != nil {
fmt.Printf("Error resending traffic from VPN server to TUN %s: %v", tun.Name(), err)
fmt.Printf("Error resending traffic from VPN server to TUN %s: %v\n", tun.Name(), err)
}
}()

Expand Down Expand Up @@ -257,10 +257,10 @@ func (c *Client) routeTrafficDirectly(tunGateway net.IP) {

// remove main route
if err := DeleteRoute(ipv4FirstHalfAddr, tunGateway.String()); err != nil {
fmt.Printf("Error routing traffic through default network gateway: %v", err)
fmt.Printf("Error routing traffic through default network gateway: %v\n", err)
}
if err := DeleteRoute(ipv4SecondHalfAddr, tunGateway.String()); err != nil {
fmt.Printf("Error routing traffic through default network gateway: %v", err)
fmt.Printf("Error routing traffic through default network gateway: %v\n", err)
}
}

Expand Down Expand Up @@ -290,7 +290,7 @@ func (c *Client) setupDirectRoute(ip net.IP) error {

func (c *Client) removeDirectRoute(ip net.IP) error {
if !ip.IsLoopback() {
fmt.Printf("Removing direct route to %s", ip.String())
fmt.Printf("Removing direct route to %s\n", ip.String())
if err := DeleteRoute(ip.String()+directRouteNetmaskCIDR, c.defaultGateway.String()); err != nil {
return err
}
Expand All @@ -306,7 +306,7 @@ func (c *Client) removeDirectRoutes() {
for _, ip := range c.directIPs {
if err := c.removeDirectRoute(ip); err != nil {
// shouldn't return, just keep on trying the other IPs
fmt.Printf("Error removing direct route to %s: %v", ip.String(), err)
fmt.Printf("Error removing direct route to %s: %v\n", ip.String(), err)
}
}
}
Expand Down Expand Up @@ -423,7 +423,7 @@ func (c *Client) shakeHands() (TUNIP, TUNGateway net.IP, err error) {
Passcode: c.cfg.Passcode,
}

fmt.Printf("Sending client hello: %v", cHello)
fmt.Printf("Sending client hello: %v\n", cHello)

if err := WriteJSON(c.conn, &cHello); err != nil {
return nil, nil, fmt.Errorf("error sending client hello: %w", err)
Expand All @@ -445,7 +445,7 @@ func (c *Client) shakeHands() (TUNIP, TUNGateway net.IP, err error) {

func (c *Client) releaseSysPrivileges(suid int) {
if err := releaseClientSysPrivileges(suid); err != nil {
fmt.Printf("Failed to release system privileges: %v", err)
fmt.Printf("Failed to release system privileges: %v\n", err)
}
}

Expand Down