|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/coder/serpent" |
| 12 | + "github.com/google/uuid" |
| 13 | + "golang.org/x/xerrors" |
| 14 | + "tailscale.com/ipn/store" |
| 15 | + "tailscale.com/tsnet" |
| 16 | +) |
| 17 | + |
| 18 | +func logF(format string, args ...any) { |
| 19 | + fmt.Printf(format+"\n", args...) |
| 20 | +} |
| 21 | + |
| 22 | +func receiveCmd() *serpent.Command { |
| 23 | + var ( |
| 24 | + // bindAddr string |
| 25 | + ) |
| 26 | + return &serpent.Command{ |
| 27 | + Use: "receive", |
| 28 | + Handler: func(inv *serpent.Invocation) error { |
| 29 | + authID := uuid.New() |
| 30 | + ts, err := newTSNet("receive", authID.String()) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + fmt.Println("Your Auth ID is:", authID.String()) |
| 36 | + |
| 37 | + ls, err := ts.Listen("tcp", "100.1.1.1:4444") |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + wait := make(chan struct{}) |
| 43 | + |
| 44 | + go http.Serve(ls, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 45 | + defer close(wait) |
| 46 | + |
| 47 | + fiName := strings.TrimPrefix(r.URL.Path, "/") |
| 48 | + defer r.Body.Close() |
| 49 | + |
| 50 | + fi, err := os.OpenFile(fiName, os.O_CREATE|os.O_RDWR, 0644) |
| 51 | + if err != nil { |
| 52 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + _, _ = io.Copy(fi, r.Body) |
| 57 | + fi.Close() |
| 58 | + |
| 59 | + w.WriteHeader(http.StatusOK) |
| 60 | + w.Write([]byte(fmt.Sprintf("file %q written", fiName))) |
| 61 | + fmt.Printf("Received file %s from %s\n", fiName, r.RemoteAddr) |
| 62 | + })) |
| 63 | + |
| 64 | + <-wait |
| 65 | + time.Sleep(1 * time.Second) |
| 66 | + |
| 67 | + // lc, _ := ts.LocalClient() |
| 68 | + // status, err := lc.Status(inv.Context()) |
| 69 | + // if err != nil { |
| 70 | + // return err |
| 71 | + // } |
| 72 | + // spew.Dump(status) |
| 73 | + return nil |
| 74 | + }, |
| 75 | + Options: []serpent.Option{ |
| 76 | + // { |
| 77 | + // Flag: "bind", |
| 78 | + // Default: "localhost:8080", |
| 79 | + // Value: serpent.StringOf(&bindAddr), |
| 80 | + // }, |
| 81 | + }, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func newTSNet(direction string, authkey string) (*tsnet.Server, error) { |
| 86 | + var err error |
| 87 | + tmp := os.TempDir() |
| 88 | + srv := new(tsnet.Server) |
| 89 | + srv.Dir = tmp |
| 90 | + srv.Hostname = "wgsh-test-" + direction |
| 91 | + srv.Ephemeral = true |
| 92 | + srv.AuthKey = direction + "-" + authkey |
| 93 | + srv.ControlURL = "http://localhost:8080" |
| 94 | + srv.Store, err = store.New(logF, "mem:lol") |
| 95 | + // srv.Logf = logF |
| 96 | + |
| 97 | + if err != nil { |
| 98 | + return nil, xerrors.Errorf("create state store: %w", err) |
| 99 | + } |
| 100 | + |
| 101 | + srv.Start() |
| 102 | + |
| 103 | + // lc, _ := srv.LocalClient() |
| 104 | + // lc.Status() |
| 105 | + // srv.TailscaleIPs() |
| 106 | + return srv, nil |
| 107 | +} |
0 commit comments