diff options
author | Mike Crute <mike@crute.us> | 2023-07-15 16:58:36 -0700 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2023-07-15 16:58:36 -0700 |
commit | fea07831eadd35532055ec16fc43b0cde56a54b1 (patch) | |
tree | 35c304a16e5b6f9acadb9c66f4c48215ad9abd2b /main.go | |
parent | 51949f8dc563c7c1ce03d8862abbee4cc1e20943 (diff) | |
download | websocket_proxy-fea07831eadd35532055ec16fc43b0cde56a54b1.tar.bz2 websocket_proxy-fea07831eadd35532055ec16fc43b0cde56a54b1.tar.xz websocket_proxy-fea07831eadd35532055ec16fc43b0cde56a54b1.zip |
Cleanup, add local client
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -50,6 +50,32 @@ var clientCmd = &cobra.Command{ | |||
50 | }, | 50 | }, |
51 | } | 51 | } |
52 | 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 | |||
70 | h := &LocalClientHandler{ | ||
71 | WebsocketServer: args[0], | ||
72 | Context: ctx, | ||
73 | } | ||
74 | |||
75 | h.Run() | ||
76 | }, | ||
77 | } | ||
78 | |||
53 | var serverCmd = &cobra.Command{ | 79 | var serverCmd = &cobra.Command{ |
54 | Use: "server [next-hop host]", | 80 | Use: "server [next-hop host]", |
55 | Short: "Serve websocket proxy client", | 81 | Short: "Serve websocket proxy client", |
@@ -69,7 +95,10 @@ var serverCmd = &cobra.Command{ | |||
69 | } | 95 | } |
70 | 96 | ||
71 | func main() { | 97 | func main() { |
98 | log.SetOutput(os.Stderr) | ||
99 | |||
72 | rootCmd.AddCommand(clientCmd) | 100 | rootCmd.AddCommand(clientCmd) |
101 | rootCmd.AddCommand(localClientCmd) | ||
73 | rootCmd.AddCommand(serverCmd) | 102 | rootCmd.AddCommand(serverCmd) |
74 | 103 | ||
75 | clientCmd.Flags().StringP("listen", "l", ":9013", "[address]:port to bind for serving clients") | 104 | clientCmd.Flags().StringP("listen", "l", ":9013", "[address]:port to bind for serving clients") |