Browse Source

update to be more go-ish

tags/v1.3.0
AJ ONeal 3 months ago
parent
commit
79c2ac5f3d
5 changed files with 75 additions and 34 deletions
  1. 6
    2
      README.md
  2. 10
    11
      cmd/sclient/main.go
  3. 38
    0
      doc.go
  4. 3
    1
      go.mod
  5. 18
    20
      sclient.go

+ 6
- 2
README.md View File

@@ -50,14 +50,18 @@ For the moment you'll have to install go and compile `sclient` yourself:
50 50
 * <https://golang.org/doc/install#install>
51 51
 
52 52
 ```bash
53
-git clone https://git.coolaj86.com/coolaj86/sclient.go.git
53
+git clone https://git.rootprojects.org/root/sclient.go.git
54 54
 pushd sclient.go
55 55
 go build -o dist/sclient cmd/sclient/main.go
56 56
 rsync -av dist/sclient /usr/local/bin/sclient
57
+sclient example.com:443 localhost:3000
57 58
 ```
58 59
 
60
+Or
61
+
59 62
 ```bash
60
-go run cmd/sclient/main.go example.com:443 localhost:3000
63
+go get git.rootprojects.org/root/sclient.go/cmd/sclient
64
+go run git.rootprojects.org/root/sclient.go/cmd/sclient example.com:443 localhost:3000
61 65
 ```
62 66
 
63 67
 Usage

+ 10
- 11
cmd/sclient/main.go View File

@@ -7,7 +7,7 @@ import (
7 7
 	"strconv"
8 8
 	"strings"
9 9
 
10
-	sclient "git.coolaj86.com/coolaj86/sclient.go"
10
+	sclient "git.rootprojects.org/root/sclient.go"
11 11
 )
12 12
 
13 13
 func usage() {
@@ -42,7 +42,7 @@ func main() {
42 42
 		}
43 43
 	}
44 44
 
45
-	opts := &sclient.PipeOpts{
45
+	sclient := &sclient.Tunnel{
46 46
 		RemotePort:         443,
47 47
 		LocalAddress:       "localhost",
48 48
 		InsecureSkipVerify: *insecure,
@@ -57,17 +57,17 @@ func main() {
57 57
 			usage()
58 58
 			os.Exit(0)
59 59
 		}
60
-		opts.RemotePort = rport
60
+		sclient.RemotePort = rport
61 61
 	} else if 1 != len(remote) {
62 62
 		usage()
63 63
 		os.Exit(0)
64 64
 	}
65
-	opts.RemoteAddress = remote[0]
65
+	sclient.RemoteAddress = remote[0]
66 66
 
67 67
 	if "-" == localstr || "|" == localstr {
68 68
 		// User may specify stdin/stdout instead of net
69
-		opts.LocalAddress = localstr
70
-		opts.LocalPort = -1
69
+		sclient.LocalAddress = localstr
70
+		sclient.LocalPort = -1
71 71
 	} else {
72 72
 		// Test that argument is a local address
73 73
 		local := strings.Split(localstr, ":")
@@ -78,20 +78,19 @@ func main() {
78 78
 				usage()
79 79
 				os.Exit(0)
80 80
 			}
81
-			opts.LocalPort = lport
81
+			sclient.LocalPort = lport
82 82
 		} else {
83 83
 			lport, err := strconv.Atoi(local[1])
84 84
 			if nil != err {
85 85
 				usage()
86 86
 				os.Exit(0)
87 87
 			}
88
-			opts.LocalAddress = local[0]
89
-			opts.LocalPort = lport
88
+			sclient.LocalAddress = local[0]
89
+			sclient.LocalPort = lport
90 90
 		}
91 91
 	}
92 92
 
93
-	sclient := &sclient.Tun{}
94
-	err := sclient.DialAndListen(opts)
93
+	err := sclient.DialAndListen()
95 94
 	if nil != err {
96 95
 		fmt.Fprintf(os.Stderr, "%s\n", err)
97 96
 		//usage()

+ 38
- 0
doc.go View File

@@ -0,0 +1,38 @@
1
+/*
2
+Package sclient unwraps SSL.
3
+
4
+It makes secure remote connections (such as HTTPS) available locally as plain-text connections -
5
+similar to `stunnel` or `openssl s_client`.
6
+
7
+There are a variety of reasons that you might want to do that,
8
+but we created it specifically to be able to upgrade applications with legacy
9
+security protocols - like SSH, OpenVPN, and Postgres - to be able to take
10
+advantage of the features modern TLS, such as ALPN and SNI
11
+(which makes them routable through almost every type of firewall).
12
+
13
+See https://telebit.cloud/sclient for more info.
14
+
15
+Try the CLI
16
+
17
+	go get git.rootprojects.org/root/sclient.go/cmd/sclient
18
+	go run git.rootprojects.org/root/sclient.go/cmd/sclient example.com:443 localhost:3000
19
+
20
+Package Basics
21
+
22
+	servername := "example.com"
23
+
24
+	sclient := &sclient.Tunnel{
25
+		ServerName:         servername,
26
+		RemoteAddress:      servername,
27
+		RemotePort:         443,
28
+		LocalAddress:       "localhost",
29
+		LocalPort:          3000,
30
+	}
31
+
32
+	err := sclient.DialAndListen()
33
+
34
+Pre-built versions for various platforms are also available at
35
+https://telebit.cloud/sclient
36
+
37
+*/
38
+package sclient

+ 3
- 1
go.mod View File

@@ -1 +1,3 @@
1
-module git.coolaj86.com/coolaj86/sclient.go
1
+module git.rootprojects.org/root/sclient.go
2
+
3
+go 1.12

+ 18
- 20
sclient.go View File

@@ -35,12 +35,12 @@ func (rw *stdnet) RemoteAddr() net.Addr {
35 35
 }
36 36
 
37 37
 // not all of net.Conn, just RWC and RemoteAddr()
38
-type Rwc interface {
38
+type netReadWriteCloser interface {
39 39
 	io.ReadWriteCloser
40 40
 	RemoteAddr() net.Addr
41 41
 }
42 42
 
43
-type PipeOpts struct {
43
+type Tunnel struct {
44 44
 	RemoteAddress      string
45 45
 	RemotePort         int
46 46
 	LocalAddress       string
@@ -49,9 +49,7 @@ type PipeOpts struct {
49 49
 	ServerName         string
50 50
 }
51 51
 
52
-type Tun struct{}
53
-
54
-func pipe(r Rwc, w Rwc, t string) {
52
+func pipe(r netReadWriteCloser, w netReadWriteCloser, t string) {
55 53
 	buffer := make([]byte, 2048)
56 54
 	for {
57 55
 		done := false
@@ -87,11 +85,11 @@ func pipe(r Rwc, w Rwc, t string) {
87 85
 	}
88 86
 }
89 87
 
90
-func handleConnection(remote string, conn Rwc, opts *PipeOpts) {
88
+func (t *Tunnel) handleConnection(remote string, conn netReadWriteCloser) {
91 89
 	sclient, err := tls.Dial("tcp", remote,
92 90
 		&tls.Config{
93
-			ServerName:         opts.ServerName,
94
-			InsecureSkipVerify: opts.InsecureSkipVerify,
91
+			ServerName:         t.ServerName,
92
+			InsecureSkipVerify: t.InsecureSkipVerify,
95 93
 		})
96 94
 
97 95
 	if err != nil {
@@ -102,22 +100,22 @@ func handleConnection(remote string, conn Rwc, opts *PipeOpts) {
102 100
 
103 101
 	if "stdio" == conn.RemoteAddr().Network() {
104 102
 		fmt.Fprintf(os.Stdout, "(connected to %s:%d and reading from %s)\n",
105
-			opts.RemoteAddress, opts.RemotePort, conn.RemoteAddr().String())
103
+			t.RemoteAddress, t.RemotePort, conn.RemoteAddr().String())
106 104
 	} else {
107 105
 		fmt.Fprintf(os.Stdout, "[connect] %s => %s:%d\n",
108
-			strings.Replace(conn.RemoteAddr().String(), "[::1]:", "localhost:", 1), opts.RemoteAddress, opts.RemotePort)
106
+			strings.Replace(conn.RemoteAddr().String(), "[::1]:", "localhost:", 1), t.RemoteAddress, t.RemotePort)
109 107
 	}
110 108
 
111 109
 	go pipe(conn, sclient, "local")
112 110
 	pipe(sclient, conn, "remote")
113 111
 }
114 112
 
115
-func (*Tun) DialAndListen(opts *PipeOpts) error {
116
-	remote := opts.RemoteAddress + ":" + strconv.Itoa(opts.RemotePort)
113
+func (t *Tunnel) DialAndListen() error {
114
+	remote := t.RemoteAddress + ":" + strconv.Itoa(t.RemotePort)
117 115
 	conn, err := tls.Dial("tcp", remote,
118 116
 		&tls.Config{
119
-			ServerName:         opts.ServerName,
120
-			InsecureSkipVerify: opts.InsecureSkipVerify,
117
+			ServerName:         t.ServerName,
118
+			InsecureSkipVerify: t.InsecureSkipVerify,
121 119
 		})
122 120
 
123 121
 	if err != nil {
@@ -127,28 +125,28 @@ func (*Tun) DialAndListen(opts *PipeOpts) error {
127 125
 	}
128 126
 
129 127
 	// use stdin/stdout
130
-	if "-" == opts.LocalAddress || "|" == opts.LocalAddress {
128
+	if "-" == t.LocalAddress || "|" == t.LocalAddress {
131 129
 		var name string
132 130
 		network := "stdio"
133
-		if "|" == opts.LocalAddress {
131
+		if "|" == t.LocalAddress {
134 132
 			name = "pipe"
135 133
 		} else {
136 134
 			name = "stdin"
137 135
 		}
138 136
 		conn := &stdnet{os.Stdin, os.Stdout, &stdaddr{net.UnixAddr{name, network}}}
139
-		handleConnection(remote, conn, opts)
137
+		t.handleConnection(remote, conn)
140 138
 		return nil
141 139
 	}
142 140
 
143 141
 	// use net.Conn
144
-	local := opts.LocalAddress + ":" + strconv.Itoa(opts.LocalPort)
142
+	local := t.LocalAddress + ":" + strconv.Itoa(t.LocalPort)
145 143
 	ln, err := net.Listen("tcp", local)
146 144
 	if err != nil {
147 145
 		return err
148 146
 	}
149 147
 
150 148
 	fmt.Fprintf(os.Stdout, "[listening] %s:%d <= %s:%d\n",
151
-		opts.RemoteAddress, opts.RemotePort, opts.LocalAddress, opts.LocalPort)
149
+		t.RemoteAddress, t.RemotePort, t.LocalAddress, t.LocalPort)
152 150
 
153 151
 	for {
154 152
 		conn, err := ln.Accept()
@@ -156,6 +154,6 @@ func (*Tun) DialAndListen(opts *PipeOpts) error {
156 154
 			fmt.Fprintf(os.Stderr, "[error] %s\n", err)
157 155
 			continue
158 156
 		}
159
-		go handleConnection(remote, conn, opts)
157
+		go t.handleConnection(remote, conn)
160 158
 	}
161 159
 }

Loading…
Cancel
Save