first version of rpc server.
This commit is contained in:
@@ -4,20 +4,24 @@ import (
|
||||
"git-indra.lan/indra-labs/indra"
|
||||
"git-indra.lan/indra-labs/indra/pkg/cfg"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
"git-indra.lan/indra-labs/indra/pkg/rpc"
|
||||
"git-indra.lan/indra-labs/indra/pkg/seed"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
key string
|
||||
listeners []string
|
||||
seeds []string
|
||||
connectors []string
|
||||
rpc_listeners []string
|
||||
rpc_key string
|
||||
rpc_whitelist []string
|
||||
key string
|
||||
listeners []string
|
||||
seeds []string
|
||||
connectors []string
|
||||
rpc_listen_port uint16
|
||||
rpc_key string
|
||||
rpc_whitelist_peer []string
|
||||
pc_whitelist_ip []string
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -26,17 +30,19 @@ func init() {
|
||||
seedCmd.PersistentFlags().StringSliceVarP(&listeners, "listen", "l", []string{"/ip4/127.0.0.1/tcp/8337", "/ip6/::1/tcp/8337"}, "binds to an interface")
|
||||
seedCmd.PersistentFlags().StringSliceVarP(&seeds, "seed", "s", []string{}, "adds an additional seed connection (e.g /dns4/seed0.indra.org/tcp/8337/p2p/<pub_key>)")
|
||||
seedCmd.PersistentFlags().StringSliceVarP(&connectors, "connect", "c", []string{}, "connects only to the seed multi-addresses specified")
|
||||
seedCmd.PersistentFlags().StringSliceVarP(&rpc_listeners, "rpc-listen", "", []string{}, "binds rpc server to an interface (e.g /ip4/127.0.0.1/udp/<random_port>/quic)")
|
||||
seedCmd.PersistentFlags().Uint16VarP(&rpc_listen_port, "rpc-listen-port", "", 0, "binds the udp server to port (random if not selected)")
|
||||
seedCmd.PersistentFlags().StringVarP(&rpc_key, "rpc-key", "", "", "the base58 encoded pre-shared key for accessing the rpc")
|
||||
seedCmd.PersistentFlags().StringSliceVarP(&rpc_whitelist, "rpc-whitelist", "", []string{}, "adds a peer id to the whitelist for access")
|
||||
seedCmd.PersistentFlags().StringSliceVarP(&rpc_whitelist_peer, "rpc-whitelist-peer", "", []string{}, "adds a peer id to the whitelist for access")
|
||||
seedCmd.PersistentFlags().StringSliceVarP(&pc_whitelist_ip, "rpc-whitelist-ip", "", []string{}, "adds a cidr ip range to the whitelist for access (e.g /ip4/127.0.0.1/ipcidr/32)")
|
||||
|
||||
viper.BindPFlag("key", seedCmd.PersistentFlags().Lookup("key"))
|
||||
viper.BindPFlag("listen", seedCmd.PersistentFlags().Lookup("listen"))
|
||||
viper.BindPFlag("seed", seedCmd.PersistentFlags().Lookup("seed"))
|
||||
viper.BindPFlag("connect", seedCmd.PersistentFlags().Lookup("connect"))
|
||||
viper.BindPFlag("rpc-listen", seedCmd.PersistentFlags().Lookup("rpc-listen"))
|
||||
viper.BindPFlag("rpc-listen-port", seedCmd.PersistentFlags().Lookup("rpc-listen-port"))
|
||||
viper.BindPFlag("rpc-key", seedCmd.PersistentFlags().Lookup("rpc-key"))
|
||||
viper.BindPFlag("rpc-whitelist", seedCmd.PersistentFlags().Lookup("rpc-whitelist"))
|
||||
viper.BindPFlag("rpc-whitelist-peer", seedCmd.PersistentFlags().Lookup("rpc-whitelist-peer"))
|
||||
viper.BindPFlag("rpc-whitelist-ip", seedCmd.PersistentFlags().Lookup("rpc-whitelist-ip"))
|
||||
|
||||
rootCmd.AddCommand(seedCmd)
|
||||
}
|
||||
@@ -54,6 +60,33 @@ var seedCmd = &cobra.Command{
|
||||
|
||||
config.Params = cfg.SelectNetworkParams(viper.GetString("network"))
|
||||
|
||||
config.RPCConfig.Key.Decode(viper.GetString("rpc-key"))
|
||||
|
||||
if config.RPCConfig.IsEnabled() {
|
||||
|
||||
config.RPCConfig.ListenPort = viper.GetUint16("rpc-listen-port")
|
||||
|
||||
if config.RPCConfig.ListenPort == 0 {
|
||||
rand.Seed(time.Now().Unix())
|
||||
config.RPCConfig.ListenPort = uint16(rand.Intn(45534) + 10000)
|
||||
|
||||
viper.Set("rpc-listen-port", config.RPCConfig.ListenPort)
|
||||
}
|
||||
|
||||
for _, ip := range viper.GetStringSlice("rpc-whitelist-ip") {
|
||||
config.RPCConfig.IP_Whitelist = append(config.RPCConfig.IP_Whitelist, multiaddr.StringCast(ip))
|
||||
}
|
||||
|
||||
for _, peer := range viper.GetStringSlice("rpc-whitelist-peer") {
|
||||
|
||||
var pubKey rpc.RPCPublicKey
|
||||
|
||||
pubKey.Decode(peer)
|
||||
|
||||
config.RPCConfig.Peer_Whitelist = append(config.RPCConfig.Peer_Whitelist, pubKey)
|
||||
}
|
||||
}
|
||||
|
||||
if config.PrivKey, err = seed.GetOrGeneratePrivKey(viper.GetString("key")); check(err) {
|
||||
return
|
||||
}
|
||||
|
||||
14
go.mod
14
go.mod
@@ -23,6 +23,7 @@ require (
|
||||
github.com/spf13/viper v1.15.0
|
||||
github.com/templexxx/reedsolomon v1.1.3
|
||||
go.uber.org/atomic v1.10.0
|
||||
golang.zx2c4.com/wireguard v0.0.0-20230216153314-c7b76d3d9ecd
|
||||
gopkg.in/src-d/go-git.v4 v4.13.1
|
||||
)
|
||||
|
||||
@@ -69,6 +70,7 @@ require (
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||
github.com/golang/mock v1.6.0 // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/google/btree v1.0.1 // indirect
|
||||
github.com/google/gopacket v1.1.19 // indirect
|
||||
github.com/google/pprof v0.0.0-20221203041831-ce31453925ec // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
@@ -144,7 +146,7 @@ require (
|
||||
github.com/opencontainers/go-digest v1.0.0 // indirect
|
||||
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
|
||||
github.com/opencontainers/runc v1.1.4 // indirect
|
||||
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
|
||||
github.com/opencontainers/runtime-spec v1.0.3-0.20211123151946-c2389c3cb60a // indirect
|
||||
github.com/opentracing/opentracing-go v1.2.0 // indirect
|
||||
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
|
||||
@@ -175,21 +177,23 @@ require (
|
||||
go.uber.org/fx v1.18.2 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
go.uber.org/zap v1.24.0 // indirect
|
||||
golang.org/x/crypto v0.3.0 // indirect
|
||||
golang.org/x/crypto v0.6.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
|
||||
golang.org/x/mod v0.7.0 // indirect
|
||||
golang.org/x/net v0.5.0 // indirect
|
||||
golang.org/x/net v0.7.0 // indirect
|
||||
golang.org/x/sync v0.1.0 // indirect
|
||||
golang.org/x/sys v0.5.0 // indirect
|
||||
golang.org/x/term v0.4.0 // indirect
|
||||
golang.org/x/term v0.5.0 // indirect
|
||||
golang.org/x/text v0.7.0 // indirect
|
||||
golang.org/x/time v0.1.0 // indirect
|
||||
golang.org/x/tools v0.5.0 // indirect
|
||||
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect
|
||||
google.golang.org/protobuf v1.28.1 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/src-d/go-billy.v4 v4.3.2 // indirect
|
||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
gotest.tools/v3 v3.4.0 // indirect
|
||||
gvisor.dev/gvisor v0.0.0-20221203005347-703fd9b7fbc0 // indirect
|
||||
lukechampine.com/blake3 v1.1.7 // indirect
|
||||
)
|
||||
|
||||
|
||||
25
go.sum
25
go.sum
@@ -503,6 +503,8 @@ github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
|
||||
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
|
||||
github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4=
|
||||
github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
@@ -911,8 +913,9 @@ github.com/opencontainers/runtime-spec v1.0.1/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/
|
||||
github.com/opencontainers/runtime-spec v1.0.2-0.20190207185410-29686dbc5559/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
|
||||
github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
|
||||
github.com/opencontainers/runtime-spec v1.0.3-0.20200929063507-e6143ca7d51d/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
|
||||
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 h1:3snG66yBm59tKhhSPQrQ/0bCrv1LQbKt40LnUPiUxdc=
|
||||
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
|
||||
github.com/opencontainers/runtime-spec v1.0.3-0.20211123151946-c2389c3cb60a h1:9iT75RHhYHWwWRlVWU7wnmtFulYcURCglzQOpT+cAF8=
|
||||
github.com/opencontainers/runtime-spec v1.0.3-0.20211123151946-c2389c3cb60a/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
|
||||
github.com/opencontainers/runtime-tools v0.0.0-20181011054405-1d69bd0f9c39/go.mod h1:r3f7wjNzSs2extwzU3Y+6pKfobzPh+kKFJ3ofN+3nfs=
|
||||
github.com/opencontainers/selinux v1.6.0/go.mod h1:VVGKuOLlE7v4PJyT6h7mNWvq1rzqiriPsEqVhc+svHE=
|
||||
github.com/opencontainers/selinux v1.8.0/go.mod h1:RScLhm78qiWa2gbVCcGkC7tCGdgk3ogry1nUQF8Evvo=
|
||||
@@ -1209,8 +1212,8 @@ golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWP
|
||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
|
||||
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
|
||||
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
|
||||
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
|
||||
@@ -1307,8 +1310,8 @@ golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qx
|
||||
golang.org/x/net v0.0.0-20210825183410-e898025ed96a/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||
golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=
|
||||
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
|
||||
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
|
||||
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
@@ -1443,8 +1446,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.4.0 h1:O7UWfv5+A2qiuulQk30kVinPoMtoIPeVaKLEgLpVkvg=
|
||||
golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ=
|
||||
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
@@ -1462,6 +1465,7 @@ golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxb
|
||||
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA=
|
||||
golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
@@ -1535,6 +1539,10 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 h1:B82qJJgjvYKsXS9jeunTOisW56dUokqW/FOteYJJ/yg=
|
||||
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2/go.mod h1:deeaetjYA+DHMHg+sMSMI58GrEteJUUzzw7en6TJQcI=
|
||||
golang.zx2c4.com/wireguard v0.0.0-20230216153314-c7b76d3d9ecd h1:thMXEWXMWIiGlp5T/V+CoetkzBJi4INNaglxdvyfK0c=
|
||||
golang.zx2c4.com/wireguard v0.0.0-20230216153314-c7b76d3d9ecd/go.mod h1:whfbyDBt09xhCYQWtO2+3UVjlaq6/9hDZrjg2ZE6SyA=
|
||||
google.golang.org/api v0.0.0-20160322025152-9bf6e6e569ff/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
|
||||
google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
|
||||
google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
|
||||
@@ -1703,8 +1711,9 @@ gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81
|
||||
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
|
||||
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
|
||||
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=
|
||||
gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g=
|
||||
grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o=
|
||||
gvisor.dev/gvisor v0.0.0-20221203005347-703fd9b7fbc0 h1:Wobr37noukisGxpKo5jAsLREcpj61RxrWYzD8uwveOY=
|
||||
gvisor.dev/gvisor v0.0.0-20221203005347-703fd9b7fbc0/go.mod h1:Dn5idtptoW1dIos9U6A2rpebLs/MtTwFacjKb8jLdQA=
|
||||
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
||||
81
pkg/rpc/keys.go
Normal file
81
pkg/rpc/keys.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"github.com/btcsuite/btcd/btcutil/base58"
|
||||
"golang.org/x/crypto/curve25519"
|
||||
"golang.zx2c4.com/wireguard/device"
|
||||
)
|
||||
|
||||
const (
|
||||
RPCPrivateKeySize = 32
|
||||
RPCPublicKeySize = 32
|
||||
)
|
||||
|
||||
type (
|
||||
RPCPrivateKey [RPCPrivateKeySize]byte
|
||||
RPCPublicKey [RPCPublicKeySize]byte
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultRPCPrivateKey RPCPrivateKey
|
||||
DefaultRPCPublicKey RPCPublicKey
|
||||
)
|
||||
|
||||
func NewPrivateKey() (sk RPCPrivateKey, err error) {
|
||||
|
||||
_, err = rand.Read(sk[:])
|
||||
|
||||
sk[0] &= 248
|
||||
sk[31] = (sk[31] & 127) | 64
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (key RPCPrivateKey) Equals(tar RPCPrivateKey) bool {
|
||||
return subtle.ConstantTimeCompare(key[:], tar[:]) == 1
|
||||
}
|
||||
|
||||
func (sk RPCPrivateKey) IsZero() bool {
|
||||
var zero RPCPrivateKey
|
||||
return sk.Equals(zero)
|
||||
}
|
||||
|
||||
func (sk *RPCPrivateKey) PubKey() (pk RPCPublicKey) {
|
||||
apk := (*[device.NoisePublicKeySize]byte)(&pk)
|
||||
ask := (*[RPCPrivateKeySize]byte)(sk)
|
||||
curve25519.ScalarBaseMult(apk, ask)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (sk *RPCPrivateKey) AsDeviceKey() device.NoisePrivateKey {
|
||||
return device.NoisePrivateKey(*sk)
|
||||
}
|
||||
|
||||
func (sk RPCPrivateKey) Encode() (key string) {
|
||||
|
||||
key = base58.Encode(sk[:])
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (sk *RPCPrivateKey) Decode(key string) {
|
||||
copy(sk[:], base58.Decode(key))
|
||||
}
|
||||
|
||||
func (sk RPCPublicKey) AsDeviceKey() device.NoisePublicKey {
|
||||
return device.NoisePublicKey(sk)
|
||||
}
|
||||
|
||||
func (sk RPCPublicKey) Encode() (key string) {
|
||||
|
||||
key = base58.Encode(sk[:])
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (sk RPCPublicKey) Decode(key string) {
|
||||
copy(sk[:], base58.Decode(key))
|
||||
}
|
||||
76
pkg/rpc/rpc.go
Normal file
76
pkg/rpc/rpc.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"git-indra.lan/indra-labs/indra"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
"golang.zx2c4.com/wireguard/conn"
|
||||
"golang.zx2c4.com/wireguard/device"
|
||||
"golang.zx2c4.com/wireguard/tun"
|
||||
"golang.zx2c4.com/wireguard/tun/netstack"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
log = log2.GetLogger(indra.PathBase)
|
||||
check = log.E.Chk
|
||||
)
|
||||
|
||||
type RPCConfig struct {
|
||||
Key *RPCPrivateKey
|
||||
ListenPort uint16
|
||||
Peer_Whitelist []RPCPublicKey
|
||||
IP_Whitelist []multiaddr.Multiaddr
|
||||
}
|
||||
|
||||
func (conf *RPCConfig) IsEnabled() bool {
|
||||
return !conf.Key.IsZero()
|
||||
}
|
||||
|
||||
type RPC struct {
|
||||
device *device.Device
|
||||
}
|
||||
|
||||
func (rpc *RPC) Start() {
|
||||
rpc.device.Up()
|
||||
}
|
||||
|
||||
func (rpc *RPC) Stop() {
|
||||
|
||||
rpc.device.Close()
|
||||
}
|
||||
|
||||
func New(config *RPCConfig) (*RPC, error) {
|
||||
|
||||
var err error
|
||||
var r RPC
|
||||
|
||||
var tunnel tun.Device
|
||||
//var network *netstack.Net
|
||||
|
||||
if tunnel, _, err = netstack.CreateNetTUN([]netip.Addr{}, []netip.Addr{}, 1420); check(err) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r.device = device.NewDevice(tunnel, conn.NewDefaultBind(), device.NewLogger(device.LogLevelVerbose, ""))
|
||||
|
||||
r.device.SetPrivateKey(config.Key.AsDeviceKey())
|
||||
r.device.IpcSet("listen_port=" + strconv.Itoa(int(config.ListenPort)))
|
||||
|
||||
var peer *device.Peer
|
||||
|
||||
for _, peer_whitelist := range config.Peer_Whitelist {
|
||||
|
||||
if peer, err = r.device.NewPeer(peer_whitelist.AsDeviceKey()); check(err) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
spew.Dump(peer.String())
|
||||
peer.Start()
|
||||
|
||||
}
|
||||
|
||||
return &r, nil
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package seed
|
||||
|
||||
import (
|
||||
"git-indra.lan/indra-labs/indra/pkg/cfg"
|
||||
"git-indra.lan/indra-labs/indra/pkg/rpc"
|
||||
"github.com/libp2p/go-libp2p/core/crypto"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
)
|
||||
@@ -21,6 +22,12 @@ var DefaultConfig = &Config{
|
||||
ListenAddresses: []multiaddr.Multiaddr{},
|
||||
SeedAddresses: []multiaddr.Multiaddr{},
|
||||
ConnectAddresses: []multiaddr.Multiaddr{},
|
||||
RPCConfig: &rpc.RPCConfig{
|
||||
Key: &rpc.DefaultRPCPrivateKey,
|
||||
ListenPort: 0,
|
||||
Peer_Whitelist: []rpc.RPCPublicKey{},
|
||||
IP_Whitelist: []multiaddr.Multiaddr{},
|
||||
},
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@@ -32,4 +39,6 @@ type Config struct {
|
||||
ListenAddresses []multiaddr.Multiaddr
|
||||
|
||||
Params *cfg.Params
|
||||
|
||||
RPCConfig *rpc.RPCConfig
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package seed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git-indra.lan/indra-labs/indra/pkg/rpc"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/libp2p/go-libp2p"
|
||||
@@ -30,6 +32,8 @@ type Server struct {
|
||||
config *Config
|
||||
|
||||
host host.Host
|
||||
|
||||
rpc *rpc.RPC
|
||||
}
|
||||
|
||||
func (srv *Server) Restart() (err error) {
|
||||
@@ -56,6 +60,8 @@ func (srv *Server) Serve() (err error) {
|
||||
|
||||
log.I.Ln("starting the server")
|
||||
|
||||
srv.rpc.Start()
|
||||
|
||||
// Here we create a context with cancel and add it to the interrupt handler
|
||||
var ctx context.Context
|
||||
var cancel context.CancelFunc
|
||||
@@ -87,14 +93,29 @@ func (srv *Server) Serve() (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func New(config *Config) (srv *Server, err error) {
|
||||
func New(config *Config) (*Server, error) {
|
||||
|
||||
log.I.Ln("initializing the server")
|
||||
|
||||
var err error
|
||||
var s Server
|
||||
|
||||
s.config = config
|
||||
|
||||
if s.config.RPCConfig.IsEnabled() {
|
||||
|
||||
log.I.Ln("enabling rpc server")
|
||||
|
||||
if s.rpc, err = rpc.New(s.config.RPCConfig); check(err) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.I.Ln("rpc public key:")
|
||||
log.I.Ln("-", s.config.RPCConfig.Key.PubKey().Encode())
|
||||
log.I.Ln("rpc listeners:")
|
||||
log.I.Ln("- [/ip4/0.0.0.0/udp/"+strconv.Itoa(int(s.config.RPCConfig.ListenPort)), "/ip6/::1/udp/"+strconv.Itoa(int(s.config.RPCConfig.ListenPort))+"]")
|
||||
}
|
||||
|
||||
if s.host, err = libp2p.New(libp2p.Identity(config.PrivKey), libp2p.UserAgent(userAgent), libp2p.ListenAddrs(config.ListenAddresses...)); check(err) {
|
||||
return nil, err
|
||||
}
|
||||
@@ -117,7 +138,7 @@ func New(config *Config) (srv *Server, err error) {
|
||||
var seedAddresses []multiaddr.Multiaddr
|
||||
|
||||
if seedAddresses, err = config.Params.ParseSeedMultiAddresses(); check(err) {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config.SeedAddresses = append(config.SeedAddresses, seedAddresses...)
|
||||
|
||||
Reference in New Issue
Block a user