diff options
author | Mike Crute <mike@crute.us> | 2023-07-29 12:15:13 -0700 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2023-07-29 12:15:13 -0700 |
commit | 4e995f9e6c3adc43a361b6fa9b976d25378f1594 (patch) | |
tree | 862642149583fa4ad662edfe0b31a7d65b8e302e /main.go | |
parent | fea07831eadd35532055ec16fc43b0cde56a54b1 (diff) | |
download | websocket_proxy-4e995f9e6c3adc43a361b6fa9b976d25378f1594.tar.bz2 websocket_proxy-4e995f9e6c3adc43a361b6fa9b976d25378f1594.tar.xz websocket_proxy-4e995f9e6c3adc43a361b6fa9b976d25378f1594.zip |
Initial import of rewrite
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 117 |
1 files changed, 23 insertions, 94 deletions
@@ -1,111 +1,40 @@ | |||
1 | package main | 1 | package main |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "context" | 4 | "embed" |
5 | "errors" | ||
6 | "fmt" | ||
7 | "log" | 5 | "log" |
8 | "net/http" | ||
9 | "os" | ||
10 | "strings" | ||
11 | 6 | ||
12 | "github.com/spf13/cobra" | 7 | "code.crute.us/mcrute/ssh-proxy/app" |
13 | ) | 8 | "code.crute.us/mcrute/ssh-proxy/cmd/client" |
14 | 9 | "code.crute.us/mcrute/ssh-proxy/cmd/register" | |
15 | var version string | 10 | "code.crute.us/mcrute/ssh-proxy/cmd/web" |
16 | |||
17 | var rootCmd = &cobra.Command{ | ||
18 | Use: "websocket-proxy", | ||
19 | Version: version, | ||
20 | Short: "Proxy TCP connections over a websocket", | ||
21 | } | ||
22 | |||
23 | var clientCmd = &cobra.Command{ | ||
24 | Use: "client [server host]", | ||
25 | Short: "Act as a client for a websocket-proxy server", | ||
26 | Args: func(cmd *cobra.Command, args []string) error { | ||
27 | if len(args) != 1 || args[0] == "" { | ||
28 | return errors.New("Server host is a required argument") | ||
29 | } | ||
30 | if !strings.HasPrefix(args[0], "ws://") && !strings.HasPrefix(args[0], "wss://") { | ||
31 | return errors.New("Server host format is ws[s]://host[:port]/[path]") | ||
32 | } | ||
33 | return nil | ||
34 | }, | ||
35 | Run: func(cmd *cobra.Command, args []string) { | ||
36 | listenOn := cmd.Flag("listen").Value.String() | ||
37 | |||
38 | // TODO: Handle signals | ||
39 | ctx, cancel := context.WithCancel(context.Background()) | ||
40 | defer cancel() | ||
41 | |||
42 | h := &ClientHandler{ | ||
43 | SocketListenOn: listenOn, | ||
44 | WebsocketServer: args[0], | ||
45 | Context: ctx, | ||
46 | } | ||
47 | 11 | ||
48 | log.Printf("Serving on %s", listenOn) | 12 | "code.crute.us/mcrute/golib/cli" |
49 | h.Run() | ||
50 | }, | ||
51 | } | ||
52 | |||
53 | var localClientCmd = &cobra.Command{ | ||
54 | Use: "localclient [server host]", | ||
55 | Short: "Act as a client for a websocket-proxy server", | ||
56 | Args: func(cmd *cobra.Command, args []string) error { | ||
57 | if len(args) != 1 || args[0] == "" { | ||
58 | return errors.New("Server host is a required argument") | ||
59 | } | ||
60 | if !strings.HasPrefix(args[0], "ws://") && !strings.HasPrefix(args[0], "wss://") { | ||
61 | return errors.New("Server host format is ws[s]://host[:port]/[path]") | ||
62 | } | ||
63 | return nil | ||
64 | }, | ||
65 | Run: func(cmd *cobra.Command, args []string) { | ||
66 | // TODO: Handle signals | ||
67 | ctx, cancel := context.WithCancel(context.Background()) | ||
68 | defer cancel() | ||
69 | 13 | ||
70 | h := &LocalClientHandler{ | 14 | "github.com/spf13/cobra" |
71 | WebsocketServer: args[0], | ||
72 | Context: ctx, | ||
73 | } | ||
74 | 15 | ||
75 | h.Run() | 16 | // Import backup data. By default zoneinfo is installed in the docker image |
76 | }, | 17 | // if something breaks this will still result in us having correct TZ info. |
77 | } | 18 | _ "time/tzdata" |
19 | ) | ||
78 | 20 | ||
79 | var serverCmd = &cobra.Command{ | 21 | //go:embed templates |
80 | Use: "server [next-hop host]", | 22 | var embeddedTemplates embed.FS |
81 | Short: "Serve websocket proxy client", | ||
82 | Args: func(cmd *cobra.Command, args []string) error { | ||
83 | if len(args) != 1 || args[0] == "" { | ||
84 | return errors.New("Next-hop host is a required argument") | ||
85 | } | ||
86 | return nil | ||
87 | }, | ||
88 | Run: func(cmd *cobra.Command, args []string) { | ||
89 | listenOn := cmd.Flag("listen").Value.String() | ||
90 | log.Printf("Serving on %s", listenOn) | ||
91 | 23 | ||
92 | http.Handle("/", NewServerHandler(args[0])) | 24 | var appVersion string |
93 | log.Fatal(http.ListenAndServe(listenOn, nil)) | ||
94 | }, | ||
95 | } | ||
96 | 25 | ||
97 | func main() { | 26 | func main() { |
98 | log.SetOutput(os.Stderr) | 27 | rootCmd := &cobra.Command{ |
99 | 28 | Use: "web-server", | |
100 | rootCmd.AddCommand(clientCmd) | 29 | Short: "SSH proxy web server", |
101 | rootCmd.AddCommand(localClientCmd) | 30 | } |
102 | rootCmd.AddCommand(serverCmd) | 31 | cli.AddFlags(rootCmd, &app.Config{}, app.DefaultConfig, "") |
103 | 32 | ||
104 | clientCmd.Flags().StringP("listen", "l", ":9013", "[address]:port to bind for serving clients") | 33 | web.Register(rootCmd, embeddedTemplates, appVersion) |
105 | serverCmd.Flags().StringP("listen", "l", ":9012", "[address]:port to bind for serving clients") | 34 | client.Register(rootCmd) |
35 | register.Register(rootCmd) | ||
106 | 36 | ||
107 | if err := rootCmd.Execute(); err != nil { | 37 | if err := rootCmd.Execute(); err != nil { |
108 | fmt.Println(err) | 38 | log.Fatalf("Error running root command: %s", err) |
109 | os.Exit(1) | ||
110 | } | 39 | } |
111 | } | 40 | } |