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)
}
type SSHtunnel struct {
Local *Endpoint
Server *Endpoint
Remote *Endpoint
Config *ssh.ClientConfig
}
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", "motherbrain.unr.edu",
"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")
localEndpoint := &Endpoint{
Host: "localhost",
Port: 27013,
}
log.Println( "Host:", localEndpoint.Host, "Port:" , localEndpoint.Port)
serverEndpoint := &Endpoint{
Host: *serverHostname,
Port: 22,
}
log.Println( "Host:", serverEndpoint.Host, "Port:" , serverEndpoint.Port)
remoteEndpoint := &Endpoint{
Host: *remoteHostname,
Port: *remotePort,
}
log.Println( "Host:", remoteEndpoint.Host, "Port:" , remoteEndpoint.Port)
//credChan := make(chan Credentials)
var passwordForm= NewPasswordForm()
//passwordForm.SetChan(credChan)
passwordForm.Show();
credentials := passwordForm.Credentials
log.Printf("Connecting to %s, User: %s ", *remoteHostname, 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,
}
tunnel.Start()
}

File Metadata

Mime Type
text/plain
Expires
Sun, Feb 23, 7:35 PM (4 h, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
15193
Default Alt Text
app.go (3 KB)

Event Timeline