|
| 1 | +package pinggy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "net" |
| 9 | + "net/http" |
| 10 | + "time" |
| 11 | + |
| 12 | + "golang.org/x/crypto/ssh" |
| 13 | +) |
| 14 | + |
| 15 | +type Client struct { |
| 16 | + SSH *ssh.Client |
| 17 | + TCP net.Listener |
| 18 | + API *http.Client |
| 19 | +} |
| 20 | + |
| 21 | +func NewClient(proto string) (*Client, error) { |
| 22 | + switch proto { |
| 23 | + case "http", "tcp", "tls", "tlstcp": |
| 24 | + case "": |
| 25 | + proto = "http" |
| 26 | + default: |
| 27 | + return nil, errors.New("pinggy: unsupported proto: " + proto) |
| 28 | + } |
| 29 | + |
| 30 | + config := &ssh.ClientConfig{ |
| 31 | + User: "auth+" + proto, |
| 32 | + Auth: []ssh.AuthMethod{ssh.Password("nopass")}, |
| 33 | + HostKeyCallback: ssh.InsecureIgnoreHostKey(), |
| 34 | + Timeout: 5 * time.Second, |
| 35 | + } |
| 36 | + |
| 37 | + client, err := ssh.Dial("tcp", "a.pinggy.io:443", config) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + ln, err := client.Listen("tcp", "0.0.0.0:0") |
| 43 | + if err != nil { |
| 44 | + _ = client.Close() |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + c := &Client{ |
| 49 | + SSH: client, |
| 50 | + TCP: ln, |
| 51 | + API: &http.Client{ |
| 52 | + Transport: &http.Transport{ |
| 53 | + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { |
| 54 | + return client.Dial(network, addr) |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + if proto == "http" { |
| 61 | + if err = c.NewSession(); err != nil { |
| 62 | + _ = client.Close() |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return c, nil |
| 68 | +} |
| 69 | + |
| 70 | +func (c *Client) Close() error { |
| 71 | + return errors.Join(c.SSH.Close(), c.TCP.Close()) |
| 72 | +} |
| 73 | + |
| 74 | +func (c *Client) NewSession() error { |
| 75 | + session, err := c.SSH.NewSession() |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + return session.Shell() |
| 80 | +} |
| 81 | + |
| 82 | +func (c *Client) GetURLs() ([]string, error) { |
| 83 | + res, err := c.API.Get("http://localhost:4300/urls") |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + defer res.Body.Close() |
| 88 | + |
| 89 | + var v struct { |
| 90 | + URLs []string `json:"urls"` |
| 91 | + } |
| 92 | + |
| 93 | + if err = json.NewDecoder(res.Body).Decode(&v); err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + return v.URLs, nil |
| 98 | +} |
| 99 | + |
| 100 | +func (c *Client) Proxy(address string) error { |
| 101 | + defer c.TCP.Close() |
| 102 | + |
| 103 | + for { |
| 104 | + conn, err := c.TCP.Accept() |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + go proxy(conn, address) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func proxy(conn1 net.Conn, address string) { |
| 113 | + defer conn1.Close() |
| 114 | + |
| 115 | + conn2, err := net.Dial("tcp", address) |
| 116 | + if err != nil { |
| 117 | + return |
| 118 | + } |
| 119 | + defer conn2.Close() |
| 120 | + |
| 121 | + go io.Copy(conn2, conn1) |
| 122 | + io.Copy(conn1, conn2) |
| 123 | +} |
| 124 | + |
| 125 | +// DialTLS like ssh.Dial but with TLS |
| 126 | +//func DialTLS(network, addr, sni string, config *ssh.ClientConfig) (*ssh.Client, error) { |
| 127 | +// conn, err := net.DialTimeout(network, addr, config.Timeout) |
| 128 | +// if err != nil { |
| 129 | +// return nil, err |
| 130 | +// } |
| 131 | +// conn = tls.Client(conn, &tls.Config{ServerName: sni, InsecureSkipVerify: sni == ""}) |
| 132 | +// c, chans, reqs, err := ssh.NewClientConn(conn, addr, config) |
| 133 | +// if err != nil { |
| 134 | +// return nil, err |
| 135 | +// } |
| 136 | +// return ssh.NewClient(c, chans, reqs), nil |
| 137 | +//} |
0 commit comments