-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh443client.go
More file actions
99 lines (84 loc) · 2.04 KB
/
Copy pathssh443client.go
File metadata and controls
99 lines (84 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import "bufio"
import "crypto/tls"
import "encoding/base64"
import "errors"
import "flag"
import "fmt"
import "io"
import "net"
import "os"
import "strings"
type args struct {
address string
auth string
http bool
proxy string
}
func cliArgs() args {
address := flag.String("address", "", "<host>:<port>")
auth := flag.String("auth", "", "<token>:<secret>")
proxy := flag.String("proxy", "", "<host>:<port>")
http := flag.Bool("http", false, "use http")
defaultProxy := "proxy.ssh443.com:443"
flag.Parse()
proxyAddress := *proxy
if (proxyAddress == "") {
proxyAddress = defaultProxy
}
if (*address == "" || *auth == "" ) {
flag.Usage()
os.Exit(2)
}
return args{
address: *address,
auth: *auth,
proxy: proxyAddress,
http: *http,
}
}
func getConn(proxy string, ssl bool) (net.Conn) {
var conn net.Conn
var err error
if (ssl) {
conn, err = tls.Dial("tcp", proxy, &tls.Config{})
} else {
conn, err = net.Dial("tcp", proxy)
}
if (err != nil) {
panic(err)
}
return conn
}
func sendHeaders(conn net.Conn, auth string, address string, proxy string) {
fmt.Fprintf(conn, "CONNECT " + address + " HTTP/1.1\r\n")
fmt.Fprintf(conn, "Proxy-Authorization: Basic " + auth + "\r\n")
fmt.Fprintf(conn, "Host: " + proxy + "\r\n")
fmt.Fprintf(conn, "\r\n")
}
func awaitConnectionReady(conn net.Conn) {
status, err := bufio.NewReader(conn).ReadString('\n')
if err == io.EOF {
panic(errors.New("ERROR: connection closed by remote host"))
}
if err != nil {
panic(err)
}
fields := strings.Fields(status)
if (len(fields) < 3 || fields[1] != "200") {
panic(errors.New("ERROR: proxy connection failure"))
}
}
func pipeStdIO(conn net.Conn) {
defer conn.Close()
go io.Copy(conn, os.Stdin)
io.Copy(os.Stdout, conn)
}
func main() {
args := cliArgs()
auth := base64.StdEncoding.EncodeToString([]byte(args.auth))
conn := getConn(args.proxy, !args.http)
sendHeaders(conn, auth, args.address, args.proxy)
awaitConnectionReady(conn)
pipeStdIO(conn)
}