github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/cmd/lit-af/netcmds.go (about) 1 package main 2 3 import ( 4 "encoding/hex" 5 "fmt" 6 "strconv" 7 8 "github.com/mit-dci/lit/qln" 9 10 "github.com/fatih/color" 11 "github.com/mit-dci/lit/litrpc" 12 "github.com/mit-dci/lit/lnutil" 13 ) 14 15 var sayCommand = &Command{ 16 Format: fmt.Sprintf("%s%s\n", lnutil.White("say"), lnutil.ReqColor("peer", "message")), 17 Description: "Send a message to a peer.\n", 18 ShortDescription: "Send a message to a peer.\n", 19 } 20 21 var lisCommand = &Command{ 22 Format: fmt.Sprintf("%s%s\n", lnutil.White("lis"), lnutil.OptColor("port")), 23 Description: fmt.Sprintf("Start listening for incoming connections. The port number, if omitted, defaults to 2448.\n"), 24 ShortDescription: "Start listening for incoming connections.\n", 25 } 26 27 var conCommand = &Command{ 28 Format: fmt.Sprintf("%s <%s>@<%s>[:<%s>]\n", lnutil.White("con"), lnutil.White("pubkeyhash"), lnutil.White("hostname"), lnutil.White("port")), 29 Description: fmt.Sprintf("%s\n%s\n%s\n", 30 "Make a connection to another host by connecting to their pubkeyhash", 31 "(printed when listening using the lis command), on the given host.", 32 "A port may be provided; if omitted, 2448 is used."), 33 ShortDescription: "Make a connection to another host by connecting to their pubkeyhash\n", 34 } 35 36 var graphCommand = &Command{ 37 Format: fmt.Sprintf("%s\n", lnutil.White("graph")), 38 Description: fmt.Sprintf("Dump the channel graph in graphviz DOT format\n"), 39 ShortDescription: "Shows the channel map\n", 40 } 41 42 var rcAuthCommand = &Command{ 43 Format: fmt.Sprintf("%s%s\n", lnutil.White("rcauth"), lnutil.ReqColor("pub", "auth (true|false)")), 44 Description: "Authorizes a remote peer by pubkey for sending remote control commands over LNDC\n", 45 ShortDescription: "Manages authorization for remote control\n", 46 } 47 48 // Request remote control access 49 var rcRequestCommand = &Command{ 50 Format: fmt.Sprintf("%s\n", lnutil.White("rcreq")), 51 Description: "If this lit-af key has not been authorized on the server, this command will let the server know you want access. Another client that is privileged can authorize you then.\n", 52 ShortDescription: "Requests remote control authorization\n", 53 } 54 55 // graph gets the channel map 56 func (lc *litAfClient) Graph(textArgs []string) error { 57 if len(textArgs) > 0 && textArgs[0] == "-h" { 58 fmt.Fprintf(color.Output, graphCommand.Format) 59 fmt.Fprintf(color.Output, graphCommand.Description) 60 return nil 61 } 62 63 args := new(litrpc.NoArgs) 64 reply := new(litrpc.ChannelGraphReply) 65 66 err := lc.Call("LitRPC.GetChannelMap", args, reply) 67 if err != nil { 68 return err 69 } 70 71 fmt.Fprintf(color.Output, "%s\n", reply.Graph) 72 73 return nil 74 } 75 76 // Lis starts listening. Takes args of port to listen on. 77 func (lc *litAfClient) Lis(textArgs []string) error { 78 var err error 79 80 if len(textArgs) > 0 && textArgs[0] == "-h" { 81 fmt.Fprintf(color.Output, lisCommand.Format) 82 fmt.Fprintf(color.Output, lisCommand.Description) 83 return nil 84 } 85 86 args := new(litrpc.ListenArgs) 87 reply := new(litrpc.ListeningPortsReply) 88 89 args.Port = 2448 90 if len(textArgs) > 0 { 91 args.Port, err = strconv.Atoi(textArgs[0]) 92 if err != nil { 93 return err 94 } 95 } 96 97 err = lc.Call("LitRPC.Listen", args, reply) 98 if err != nil { 99 return err 100 } 101 if len(reply.LisIpPorts) == 0 { 102 return fmt.Errorf("no listening port returned") 103 } 104 105 fmt.Fprintf(color.Output, "listening on %s@%s\n", reply.Adr, reply.LisIpPorts[0]) 106 107 return nil 108 } 109 110 func (lc *litAfClient) Connect(textArgs []string) error { 111 if len(textArgs) > 0 && textArgs[0] == "-h" { 112 fmt.Fprintf(color.Output, conCommand.Format) 113 fmt.Fprintf(color.Output, conCommand.Description) 114 return nil 115 } 116 117 args := new(litrpc.ConnectArgs) 118 reply := new(litrpc.StatusReply) 119 120 if len(textArgs) == 0 { 121 return fmt.Errorf("need: con pubkeyhash@hostname:port") 122 } 123 124 args.LNAddr = textArgs[0] 125 126 err := lc.Call("LitRPC.Connect", args, reply) 127 if err != nil { 128 return err 129 } 130 131 fmt.Fprintf(color.Output, "%s\n", reply.Status) 132 return nil 133 } 134 135 func (lc *litAfClient) Say(textArgs []string) error { 136 stopEx, err := CheckHelpCommand(sayCommand, textArgs, 2) 137 if err != nil || stopEx { 138 return err 139 } 140 141 args := new(litrpc.SayArgs) 142 reply := new(litrpc.StatusReply) 143 144 peerIdx, err := strconv.Atoi(textArgs[0]) 145 if err != nil { 146 return err 147 } 148 149 textArgs = textArgs[1:] 150 151 for _, s := range textArgs { 152 args.Message += s + " " 153 } 154 155 args.Peer = uint32(peerIdx) 156 157 err = lc.Call("LitRPC.Say", args, reply) 158 if err != nil { 159 return err 160 } 161 fmt.Fprintf(color.Output, "%s\n", reply.Status) 162 return nil 163 } 164 165 func (lc *litAfClient) RemoteControlAuth(textArgs []string) error { 166 stopEx, err := CheckHelpCommand(rcAuthCommand, textArgs, 2) 167 if err != nil || stopEx { 168 return err 169 } 170 args := new(litrpc.RCAuthArgs) 171 reply := new(litrpc.StatusReply) 172 173 args.PubKey, err = hex.DecodeString(textArgs[0]) 174 if err != nil { 175 return fmt.Errorf("Could not decode pubkey: %s", err.Error()) 176 } 177 178 args.Authorization = new(qln.RemoteControlAuthorization) 179 args.Authorization.Allowed = lnutil.YupString(textArgs[1]) 180 181 err = lc.Call("LitRPC.RemoteControlAuth", args, reply) 182 fmt.Fprintf(color.Output, "%s\n", reply.Status) 183 return nil 184 } 185 186 func (lc *litAfClient) RemoteControlRequest(textArgs []string) error { 187 stopEx, err := CheckHelpCommand(rcRequestCommand, textArgs, 0) 188 if err != nil || stopEx { 189 return err 190 } 191 args := new(litrpc.RCRequestAuthArgs) 192 reply := new(litrpc.StatusReply) 193 194 // No need to fill it in, since this will be handled 195 // by the RPC server (inserts the pubkey from the lndc connection) 196 197 err = lc.Call("LitRPC.RequestRemoteControlAuthorization", args, reply) 198 fmt.Fprintf(color.Output, "%s\n", reply.Status) 199 return nil 200 }