summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2023-07-29 12:15:13 -0700
committerMike Crute <mike@crute.us>2023-07-29 12:15:13 -0700
commit4e995f9e6c3adc43a361b6fa9b976d25378f1594 (patch)
tree862642149583fa4ad662edfe0b31a7d65b8e302e /main.go
parentfea07831eadd35532055ec16fc43b0cde56a54b1 (diff)
downloadwebsocket_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.go117
1 files changed, 23 insertions, 94 deletions
diff --git a/main.go b/main.go
index 1ac3351..3306a94 100644
--- a/main.go
+++ b/main.go
@@ -1,111 +1,40 @@
1package main 1package main
2 2
3import ( 3import (
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"
15var version string 10 "code.crute.us/mcrute/ssh-proxy/cmd/web"
16
17var rootCmd = &cobra.Command{
18 Use: "websocket-proxy",
19 Version: version,
20 Short: "Proxy TCP connections over a websocket",
21}
22
23var 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
53var 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
79var serverCmd = &cobra.Command{ 21//go:embed templates
80 Use: "server [next-hop host]", 22var 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])) 24var appVersion string
93 log.Fatal(http.ListenAndServe(listenOn, nil))
94 },
95}
96 25
97func main() { 26func 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}