-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
165 lines (149 loc) · 3.39 KB
/
main.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bytes"
"crypto/sha256"
"crypto/tls"
"encoding/hex"
"fmt"
"io"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/spf13/pflag"
)
type stdioWrapper struct {
stdin *os.File
stdout *os.File
}
func NewStdioWrapper() *stdioWrapper {
return &stdioWrapper{os.Stdin, os.Stdout}
}
func (w *stdioWrapper) Read(p []byte) (int, error) {
return w.stdin.Read(p)
}
func (w *stdioWrapper) Write(p []byte) (int, error) {
return w.stdout.Write(p)
}
func (w *stdioWrapper) Close() error {
if err := w.stdin.Close(); err != nil {
return err
}
if err := w.stdout.Close(); err != nil {
return err
}
return nil
}
type proxy struct {
target string
pingInterval time.Duration
}
func (p *proxy) handleWS(w http.ResponseWriter, r *http.Request) {
upgrader := websocket.Upgrader{}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(err)
return
}
defer conn.Close()
var (
c1 = NewWSTransportWrapper(conn)
c2 io.ReadWriteCloser
)
switch p.target {
case "-":
c2 = NewStdioWrapper()
default:
c, err := net.Dial("tcp", p.target)
if err != nil {
fmt.Println(err)
return
}
c2 = c
}
bidirectCopy(c1, c2)
}
type runtimeOptions struct {
keepalive int
listen string
listenPath string
target string
fingerprint string
// TODO: make list
header string
}
func main() {
opts := runtimeOptions{}
pflag.StringVarP(&opts.header, "header", "H", "", "Specify request header")
pflag.IntVarP(&opts.keepalive, "keepalive", "k", 0, "Set ping interval in seconds")
pflag.StringVarP(&opts.fingerprint, "fingerprint", "f", "", "Set SHA-256 fingerprint of certificate")
pflag.StringVarP(&opts.listen, "listen", "l", "", "Set listen address")
pflag.StringVarP(&opts.listenPath, "path", "p", "/ws", "Set uri path")
pflag.StringVarP(&opts.target, "target", "t", "-", "Set target to proxy or connect to")
pflag.Parse()
if opts.listen != "" {
p := proxy{
target: opts.target,
pingInterval: time.Duration(opts.keepalive) * time.Second,
}
r := mux.NewRouter()
r.HandleFunc(opts.listenPath, p.handleWS)
srv := &http.Server{
Addr: opts.listen,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r,
}
if err := srv.ListenAndServe(); err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
if opts.target == "-" {
fmt.Println("error: invalid target")
os.Exit(1)
}
d := websocket.DefaultDialer
if opts.fingerprint != "" {
fp, err := hex.DecodeString(opts.fingerprint)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
d.TLSClientConfig = &tls.Config{
VerifyConnection: func(cs tls.ConnectionState) error {
digest := sha256.Sum256(cs.PeerCertificates[0].Raw)
if bytes.Equal(fp, digest[:]) {
return nil
}
return fmt.Errorf("invalid cert: %x; expected %x", fp, digest[:])
},
}
}
var (
reqHeader = make(http.Header)
)
if opts.header != "" {
p := strings.SplitN(opts.header, ":", 2)
reqHeader.Set(p[0], p[1])
}
conn, _, err := d.Dial(opts.target, reqHeader)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var (
// TODO: fix panic
c = NewWSTransportWrapper(conn)
s = NewStdioWrapper()
)
if opts.keepalive > 0 {
go c.SetKeepAlive(time.Duration(opts.keepalive) * time.Second)
}
bidirectCopy(c, s)
}
}