-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
213 lines (187 loc) · 5.68 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"errors"
"fmt"
"log"
"os"
"os/user"
"path"
"strings"
"time"
"github.com/iamatypeofwalrus/liltunnel"
flags "github.com/jessevdk/go-flags"
)
const (
knownHostsFile = "known_hosts"
sshKey = "id_rsa"
usage = "--port-mapping 80:8080 --remote me@remote.example.com --identity ~/.ssh/liltunnel_rsa"
protocolHTTP = "http"
protocolTCP = "tcp"
defaultHTTPCacheTTL = 12 * time.Hour
)
// options represents the raw cli options exposed to the user
type options struct {
PortMapping string `long:"port-mapping" short:"p" required:"true" description:"local:remote or port. If remote is not specified local port is used"`
Remote string `long:"remote" short:"r" required:"true" description:"username@remote.example.com or remote.example.com. If username is not specified the current $USER is used"`
SSHKeyPath string `long:"identity" short:"i" required:"true" description:"private key to be used when establishing a connection to the remote (default: ~/.ssh/id_rsa)"`
KnownHostsPath string `long:"known-hosts" short:"o" required:"false" description:"known hosts file (default: ~/.ssh/known_hosts)"`
Protocol string `long:"protocol" short:"n" required:"false" description:"network protocol to use when tunneling" default:"tcp" choice:"http" choice:"tcp"`
HTTPCache bool `long:"http-cache" short:"c" description:"HTTP only. Cache all succesful responses to GET requests to disk"`
HTTPCacheTTL time.Duration `long:"http-cache-ttl" short:"t" description:"HTTP only. Expressed in seconds. Length of time to keep successful responses in cache. Defaults to 12 hours"`
HTTPCacheServeStale bool `long:"http-cache-serve-stale" short:"s" description:"HTTP only. Always return return a stale read from the cache. Handy if you need an offline mode"`
Verbose bool `long:"verbose" short:"v"`
}
// config holds all of the parsed and default values sanely set from the options
// the user provided.
type config struct {
sshKeyPath string
user string
localPort string
remotePort string
remote string
knownHosts string
protocol string
httpCache bool
httpCacheTTL time.Duration
httpCacheServeStale bool
verbose bool
}
func main() {
conf, err := newConf()
if err != nil {
fmt.Fprintf(os.Stderr, "could not parse CLI options: %v\n", err)
os.Exit(1)
}
l := log.New(
&verboseWriter{verbose: conf.verbose, writer: os.Stdout},
"",
log.Lshortfile,
)
d, err := liltunnel.NewDialer(
conf.sshKeyPath,
conf.knownHosts,
conf.user,
conf.remote,
l,
)
if err != nil {
fmt.Println("could not init dialer", err)
os.Exit(1)
}
var t liltunnel.Tunneler
var tunnelErr error
if conf.protocol == "http" {
l.Printf("initalizing http tunnler")
t, err = liltunnel.NewHTTPTunnler(
d,
conf.localPort,
conf.remotePort,
conf.httpCache,
conf.httpCacheTTL,
conf.httpCacheServeStale,
l,
)
} else {
l.Printf("initalizing tcp tunnler")
t, err = liltunnel.NewTCPTunneler(
d,
conf.localPort,
conf.remotePort,
l,
)
}
if tunnelErr != nil {
fmt.Fprintf(os.Stderr, "could not initalize tunnel: %v", err)
os.Exit(1)
}
l.Printf(
"forwarding all %v traffic sent to localhost%v to %v@%v%v",
conf.protocol,
conf.localPort,
conf.user,
conf.remote,
conf.remotePort,
)
l.Fatal(t.Tunnel())
}
func newConf() (config, error) {
opts := parseArgs()
conf := config{}
currUser, err := user.Current()
if err != nil {
return conf, err
}
// PortMapping can look like "8080:80" or "8080". In the later case we implicitly
// forward set the remote port to the same as local
ports := strings.SplitN(opts.PortMapping, ":", 2)
if len(ports) == 1 {
conf.localPort = ":" + ports[0]
conf.remotePort = ":" + ports[0]
} else {
conf.localPort = ":" + ports[0]
conf.remotePort = ":" + ports[1]
}
// User and Remote address. User's pass in a string like "username@remote.example.com"
// or "remote.example.com". If "username" is not provided we'll default to the
// OS' current user.
ur := strings.Split(opts.Remote, "@")
if len(ur) == 1 {
conf.remote = ur[0]
conf.user = currUser.Username
} else {
conf.user = ur[0]
conf.remote = ur[1]
}
// Set default private key
if opts.SSHKeyPath == "" {
conf.sshKeyPath = path.Join(
currUser.HomeDir,
".ssh",
sshKey,
)
} else {
conf.sshKeyPath = opts.SSHKeyPath
}
// Set known hosts
if opts.KnownHostsPath == "" {
conf.knownHosts = path.Join(
currUser.HomeDir,
".ssh",
knownHostsFile,
)
} else {
conf.knownHosts = opts.KnownHostsPath
}
conf.protocol = opts.Protocol
if conf.protocol == protocolTCP && (opts.HTTPCache || opts.HTTPCacheTTL != 0 || opts.HTTPCacheServeStale) {
return conf, errors.New("protocol TCP does not accept arguments http-cache, http-cache-ttl, http-cache-serve-stale")
}
if opts.Protocol == protocolHTTP {
conf.httpCache = opts.HTTPCache
conf.httpCacheTTL = opts.HTTPCacheTTL * time.Second
conf.httpCacheServeStale = opts.HTTPCacheServeStale
}
if conf.httpCache && conf.httpCacheTTL == 0*time.Nanosecond {
conf.httpCacheTTL = defaultHTTPCacheTTL
}
conf.verbose = opts.Verbose
return conf, nil
}
func parseArgs() options {
p := flags.NewNamedParser("liltunnel", flags.HelpFlag)
p.Usage = usage
opts := options{}
_, err := p.AddGroup("Options", "", &opts)
if err != nil {
panic(
fmt.Sprintf("could not add application options to group: %v", err),
)
}
_, err = p.ParseArgs(os.Args)
if err != nil {
fmt.Printf("%v\n\n", err)
p.WriteHelp(os.Stdout)
os.Exit(1)
}
return opts
}