diff --git a/app.go b/app.go index d56cc2d..ba79c19 100644 --- a/app.go +++ b/app.go @@ -1,183 +1,194 @@ package sshtunnel import ( "flag" "strings" "os" "fmt" "io" "net" "encoding/base64" "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/agent" "log" + "strconv" ) type Client struct { } type Endpoint struct { Host string Port int } func KeyPrint(dialAddr string, addr net.Addr, key ssh.PublicKey) error { fmt.Printf("%s %s %s\n", strings.Split(dialAddr, ":")[0], key.Type(), base64.StdEncoding.EncodeToString(key.Marshal())) return nil } func (endpoint *Endpoint) String() string { return fmt.Sprintf("%s:%d", endpoint.Host, endpoint.Port) } func ParseEndpointString(endpointString string) (localEp Endpoint, remoteEp Endpoint){ + var err error localEp.Host = "localhost" + + buffer := strings.Split(endpointString, ":") + localEp.Port, err = strconv.Atoi(buffer[0]) + _ = err + + + remoteEp.Host = buffer[1] + remoteEp.Port, err = strconv.Atoi(buffer[2]) + _ = err return localEp, remoteEp } type SSHtunnel struct { Local *Endpoint Server *Endpoint Remote *Endpoint Config *ssh.ClientConfig ServerConnection *ssh.ServerConfig } func (tunnel *SSHtunnel) Start() error { listener, err := net.Listen("tcp", tunnel.Local.String()) if err != nil { return err } defer listener.Close() for { conn, err := listener.Accept() if err != nil { return err } go tunnel.forward(conn) } } func (tunnel *SSHtunnel) forward(localConn net.Conn) { serverConn, err := ssh.Dial("tcp", tunnel.Server.String(), tunnel.Config) if err != nil { fmt.Printf("Server dial error: %s\n", err) return } remoteConn, err := serverConn.Dial("tcp", tunnel.Remote.String()) if err != nil { fmt.Printf("Remote dial error: %s\n", err) return } copyConn:=func(writer, reader net.Conn) { _, err:= io.Copy(writer, reader) if err != nil { fmt.Printf("io.Copy error: %s", err) } } go copyConn(localConn, remoteConn) go copyConn(remoteConn, localConn) } func SSHAgent() ssh.AuthMethod { if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil { return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers) } return nil } func check(e error) { if e != nil { panic(e) } } func(c *Client) Start(){ //Get the command line arguments remoteHostname := flag.String("remote-hostname", "172.28.128.230", "Remote hostname.") remotePort := flag.Int("remote-port", 27013, "The remote port bound through the server.") //The intermediary server for port binding serverHostname := flag.String("server", "ubuntu.cse.unr.edu", "a string") flag.Parse() // 27013 serverEndpoint := &Endpoint{ Host: *serverHostname, Port: 22, } localEndpoint := &Endpoint{ Host: "localhost", Port: 27013, } remoteEndpoint := &Endpoint{ Host: *remoteHostname, Port: *remotePort, } //5555 localEndpoint2 := &Endpoint{ Host: "localhost", Port: 5555, } remoteEndpoint2 := &Endpoint{ Host: *remoteHostname, Port: 5555, } //credChan := make(chan Credentials) var passwordForm= NewPasswordForm() //passwordForm.SetChan(credChan) passwordForm.Show(); credentials := passwordForm.Credentials log.Printf("Connecting to %s, User: %s ", *serverHostname, credentials.Username) sshConfig := &ssh.ClientConfig{ User: credentials.Username, HostKeyCallback: KeyPrint, Auth: []ssh.AuthMethod{ ssh.Password(credentials.Password), }, } tunnel := &SSHtunnel{ Config: sshConfig, Local: localEndpoint, Server: serverEndpoint, Remote: remoteEndpoint, } tunnel2 := &SSHtunnel{ Config: sshConfig, Local: localEndpoint2, Server: serverEndpoint, Remote: remoteEndpoint2, } go tunnel2.Start() tunnel.Start() } diff --git a/app_test.go b/app_test.go index edfd5b9..051b2ed 100644 --- a/app_test.go +++ b/app_test.go @@ -1,53 +1,75 @@ package sshtunnel import ( "testing" "fmt" ) //function func TestEndPointString(t *testing.T) { var testend Endpoint testend.Host = "localhost" testend.Port = 5555 // make a string and test what the string should be stringWant := "localhost:5555" returnedString := testend.String() - fmt.Printf("stringWant: %s\n", stringWant) - fmt.Printf("returnedString : %s\n", returnedString) + fmt.Printf("\nstringWant: %s\n\n", stringWant) + fmt.Printf("returnedString : %s\n\n", returnedString) // return string == stringwant // success } //function func TestEndPointParseString(t *testing.T) { - var testRemote Endpoint - var testLocal Endpoint + var testRemote, testRemote1 Endpoint + var testLocal, testLocal1 Endpoint + // make a string and test what the string should be testLocal, testRemote = ParseEndpointString("5555:motherbrain.unr.edu:5555") + testLocal1, testRemote1 = ParseEndpointString("27013:motherbrain.unr.edu:27013") + + fmt.Printf("Local: %s %d\n\n", testLocal.Host, testLocal.Port) + fmt.Printf("Remote: %s %d \n\n", testRemote.Host, testRemote.Port) - fmt.Printf("Local: %s %d\n", testLocal.Host, testLocal.Port) - fmt.Printf("Remote: %s %d \n", testRemote.Host, testRemote.Port) + fmt.Printf("Local: %s %d\n\n", testLocal1.Host, testLocal1.Port) + fmt.Printf("Remote: %s %d \n\n", testRemote1.Host, testRemote1.Port) if testLocal.Host != "localhost"{ t.Error("Invalid Local EP Hostname") } if testLocal.Port != 5555 { t.Error("Invalid Local EP Port") } - if testRemote.Host != "motherbrain.unr.edu"{ - t.Error("Invalid Local EP Hostname") + t.Error("Invalid remote EP Hostname") } if testRemote.Port != 5555 { + t.Error("Invalid remote EP Port") + } + + //////////////////////////////////////// + + if testLocal1.Host != "localhost"{ + t.Error("Invalid Local EP Hostname") + } + + if testLocal1.Port != 27013 { t.Error("Invalid Local EP Port") } + + if testRemote1.Host != "motherbrain.unr.edu"{ + t.Error("Invalid remote EP Hostname") + } + + if testRemote1.Port != 27013 { + t.Error("Invalid remote EP Port") + } // return string == stringwant // success }