|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "runtime" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + chclient "github.com/jpillora/chisel/client" |
| 16 | + "github.com/jpillora/chisel/share/cos" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + version = "dev" // Set by goreleaser |
| 21 | +) |
| 22 | + |
| 23 | +func main() { |
| 24 | + client(os.Args[1:]) |
| 25 | +} |
| 26 | + |
| 27 | +func generatePidFile() { |
| 28 | + pid := []byte(strconv.Itoa(os.Getpid())) |
| 29 | + if err := ioutil.WriteFile("outsystemscc.pid", pid, 0644); err != nil { |
| 30 | + log.Fatal(err) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +type headerFlags struct { |
| 35 | + http.Header |
| 36 | +} |
| 37 | + |
| 38 | +func (flag *headerFlags) String() string { |
| 39 | + out := "" |
| 40 | + for k, v := range flag.Header { |
| 41 | + out += fmt.Sprintf("%s: %s\n", k, v) |
| 42 | + } |
| 43 | + return out |
| 44 | +} |
| 45 | + |
| 46 | +func (flag *headerFlags) Set(arg string) error { |
| 47 | + index := strings.Index(arg, ":") |
| 48 | + if index < 0 { |
| 49 | + return fmt.Errorf(`Invalid header (%s). Should be in the format "HeaderName: HeaderContent"`, arg) |
| 50 | + } |
| 51 | + if flag.Header == nil { |
| 52 | + flag.Header = http.Header{} |
| 53 | + } |
| 54 | + key := arg[0:index] |
| 55 | + value := arg[index+1:] |
| 56 | + flag.Header.Set(key, strings.TrimSpace(value)) |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +var clientHelp = ` |
| 61 | + Usage: outsystemscc [options] <server> <remote> [remote] [remote] ... |
| 62 | +
|
| 63 | + <server> is the URL to the server. |
| 64 | +
|
| 65 | + <remote>s are remote connections tunneled through the server, each of |
| 66 | + which come in the form: |
| 67 | +
|
| 68 | + R:<local-port>:<remote-host>:<remote-port> |
| 69 | +
|
| 70 | + which does reverse port forwarding, sharing <remote-host>:<remote-port> |
| 71 | + from the client to the server's <local-port>. |
| 72 | +
|
| 73 | + example remotes |
| 74 | +
|
| 75 | + R:2222:localhost:22 |
| 76 | + R:8080:10.0.0.1:80 |
| 77 | + |
| 78 | + Options: |
| 79 | +
|
| 80 | + --keepalive, An optional keepalive interval. Since the underlying |
| 81 | + transport is HTTP, in many instances we'll be traversing through |
| 82 | + proxies, often these proxies will close idle connections. You must |
| 83 | + specify a time with a unit, for example '5s' or '2m'. Defaults |
| 84 | + to '25s' (set to 0s to disable). |
| 85 | +
|
| 86 | + --max-retry-count, Maximum number of times to retry before exiting. |
| 87 | + Defaults to unlimited. |
| 88 | +
|
| 89 | + --max-retry-interval, Maximum wait time before retrying after a |
| 90 | + disconnection. Defaults to 5 minutes. |
| 91 | +
|
| 92 | + --proxy, An optional HTTP CONNECT or SOCKS5 proxy which will be |
| 93 | + used to reach the server. Authentication can be specified |
| 94 | + inside the URL. |
| 95 | + For example, http://admin:[email protected]:8081 |
| 96 | + or: socks://admin:[email protected]:1080 |
| 97 | +
|
| 98 | + --header, Set a custom header in the form "HeaderName: HeaderContent". |
| 99 | + Can be used multiple times. (e.g --header "Foo: Bar" --header "Hello: World") |
| 100 | +
|
| 101 | + --hostname, Optionally set the 'Host' header (defaults to the host |
| 102 | + found in the server url). |
| 103 | +
|
| 104 | + --pid Generate pid file in current working directory |
| 105 | +
|
| 106 | + -v, Enable verbose logging |
| 107 | +
|
| 108 | + --help, This help text |
| 109 | +
|
| 110 | + Signals: |
| 111 | + The outsystemscc process is listening for: |
| 112 | + a SIGUSR2 to print process stats, and |
| 113 | + a SIGHUP to short-circuit the client reconnect timer |
| 114 | +
|
| 115 | + Version: |
| 116 | + ` + version + ` (` + runtime.Version() + `) |
| 117 | +` |
| 118 | + |
| 119 | +func client(args []string) { |
| 120 | + flags := flag.NewFlagSet("client", flag.ContinueOnError) |
| 121 | + config := chclient.Config{Headers: http.Header{}} |
| 122 | + flags.DurationVar(&config.KeepAlive, "keepalive", 25*time.Second, "") |
| 123 | + flags.IntVar(&config.MaxRetryCount, "max-retry-count", -1, "") |
| 124 | + flags.DurationVar(&config.MaxRetryInterval, "max-retry-interval", 0, "") |
| 125 | + flags.StringVar(&config.Proxy, "proxy", "", "") |
| 126 | + flags.Var(&headerFlags{config.Headers}, "header", "") |
| 127 | + hostname := flags.String("hostname", "", "") |
| 128 | + pid := flags.Bool("pid", false, "") |
| 129 | + verbose := flags.Bool("v", false, "") |
| 130 | + flags.Usage = func() { |
| 131 | + fmt.Print(clientHelp) |
| 132 | + os.Exit(0) |
| 133 | + } |
| 134 | + flags.Parse(args) |
| 135 | + //pull out options, put back remaining args |
| 136 | + args = flags.Args() |
| 137 | + if len(args) < 2 { |
| 138 | + log.Fatalf("A server and least one remote is required") |
| 139 | + } |
| 140 | + config.Server = args[0] |
| 141 | + config.Remotes = args[1:] |
| 142 | + //default auth |
| 143 | + if config.Auth == "" { |
| 144 | + config.Auth = os.Getenv("AUTH") |
| 145 | + } |
| 146 | + //move hostname onto headers |
| 147 | + if *hostname != "" { |
| 148 | + config.Headers.Set("Host", *hostname) |
| 149 | + } |
| 150 | + //ready |
| 151 | + c, err := chclient.NewClient(&config) |
| 152 | + if err != nil { |
| 153 | + log.Fatal(err) |
| 154 | + } |
| 155 | + c.Debug = *verbose |
| 156 | + if *pid { |
| 157 | + generatePidFile() |
| 158 | + } |
| 159 | + go cos.GoStats() |
| 160 | + ctx := cos.InterruptContext() |
| 161 | + if err := c.Start(ctx); err != nil { |
| 162 | + log.Fatal(err) |
| 163 | + } |
| 164 | + if err := c.Wait(); err != nil { |
| 165 | + log.Fatal(err) |
| 166 | + } |
| 167 | +} |
0 commit comments