Page MenuHomePhabricator

app.go
No OneTemporary

package sshtunnel
import (
"flag"
"strings"
"os"
"fmt"
"io"
"net"
"encoding/base64"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"log"
)
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){
localEp.Host = "localhost"
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()
}

File Metadata

Mime Type
text/plain
Expires
Mon, Feb 24, 3:48 AM (13 h, 2 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
15159
Default Alt Text
app.go (3 KB)

Event Timeline