Vendor Go modules

This commit is contained in:
Adam Ierymenko 2019-09-30 12:41:21 -07:00
commit c4504fd3ff
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
64 changed files with 18532 additions and 36 deletions

View file

@ -84,6 +84,8 @@ type APIStatus struct {
Clock int64
Config LocalConfig
Online bool
PeerCount int
PathCount int
Identity *Identity
InterfaceAddresses []net.IP
MappedExternalAddresses []*InetAddress
@ -197,16 +199,31 @@ func createAPIServer(basePath string, node *Node) (*http.Server, error) {
smux := http.NewServeMux()
smux.HandleFunc("/status", func(out http.ResponseWriter, req *http.Request) {
defer func() {
e := recover()
if e != nil {
_ = apiSendObj(out, req, http.StatusInternalServerError, nil)
}
}()
if !apiCheckAuth(out, req, authToken) {
return
}
apiSetStandardHeaders(out)
if req.Method == http.MethodGet || req.Method == http.MethodHead {
pathCount := 0
peers := node.Peers()
for _, p := range peers {
pathCount += len(p.Paths)
}
_ = apiSendObj(out, req, http.StatusOK, &APIStatus{
Address: node.Address(),
Clock: TimeMs(),
Config: node.LocalConfig(),
Online: node.Online(),
PeerCount: len(peers),
PathCount: pathCount,
Identity: node.Identity(),
InterfaceAddresses: node.InterfaceAddresses(),
MappedExternalAddresses: nil,
@ -223,15 +240,27 @@ func createAPIServer(basePath string, node *Node) (*http.Server, error) {
})
smux.HandleFunc("/config", func(out http.ResponseWriter, req *http.Request) {
defer func() {
e := recover()
if e != nil {
_ = apiSendObj(out, req, http.StatusInternalServerError, nil)
}
}()
if !apiCheckAuth(out, req, authToken) {
return
}
apiSetStandardHeaders(out)
if req.Method == http.MethodPost || req.Method == http.MethodPut {
var c LocalConfig
if apiReadObj(out, req, &c) == nil {
_, _ = node.SetLocalConfig(&c)
_ = apiSendObj(out, req, http.StatusOK, node.LocalConfig())
_, err := node.SetLocalConfig(&c)
if err != nil {
_ = apiSendObj(out, req, http.StatusBadRequest, nil)
} else {
_ = apiSendObj(out, req, http.StatusOK, node.LocalConfig())
}
}
} else if req.Method == http.MethodGet || req.Method == http.MethodHead {
_ = apiSendObj(out, req, http.StatusOK, node.LocalConfig())
@ -242,9 +271,17 @@ func createAPIServer(basePath string, node *Node) (*http.Server, error) {
})
smux.HandleFunc("/peer/", func(out http.ResponseWriter, req *http.Request) {
defer func() {
e := recover()
if e != nil {
_ = apiSendObj(out, req, http.StatusInternalServerError, nil)
}
}()
if !apiCheckAuth(out, req, authToken) {
return
}
apiSetStandardHeaders(out)
var queriedID Address
@ -260,13 +297,13 @@ func createAPIServer(basePath string, node *Node) (*http.Server, error) {
if req.Method == http.MethodGet || req.Method == http.MethodHead {
peers := node.Peers()
if queriedID != 0 {
p2 := make([]*Peer, 0, len(peers))
for _, p := range peers {
if p.Address == queriedID {
p2 = append(p2, p)
_ = apiSendObj(out, req, http.StatusOK, p)
return
}
}
_ = apiSendObj(out, req, http.StatusOK, p2)
_ = apiSendObj(out, req, http.StatusNotFound, nil)
} else {
_ = apiSendObj(out, req, http.StatusOK, peers)
}
@ -277,9 +314,17 @@ func createAPIServer(basePath string, node *Node) (*http.Server, error) {
})
smux.HandleFunc("/network/", func(out http.ResponseWriter, req *http.Request) {
defer func() {
e := recover()
if e != nil {
_ = apiSendObj(out, req, http.StatusInternalServerError, nil)
}
}()
if !apiCheckAuth(out, req, authToken) {
return
}
apiSetStandardHeaders(out)
var queriedID NetworkID
@ -326,9 +371,10 @@ func createAPIServer(basePath string, node *Node) (*http.Server, error) {
for _, nw := range networks {
if nw.ID() == queriedID {
_ = apiSendObj(out, req, http.StatusOK, apiNetworkFromNetwork(nw))
break
return
}
}
_ = apiSendObj(out, req, http.StatusNotFound, nil)
}
} else {
out.Header().Set("Allow", "GET, HEAD, PUT, POST")
@ -337,32 +383,53 @@ func createAPIServer(basePath string, node *Node) (*http.Server, error) {
})
smux.HandleFunc("/root/", func(out http.ResponseWriter, req *http.Request) {
defer func() {
e := recover()
if e != nil {
_ = apiSendObj(out, req, http.StatusInternalServerError, nil)
}
}()
if !apiCheckAuth(out, req, authToken) {
return
}
apiSetStandardHeaders(out)
var queriedID Address
var queriedName string
if len(req.URL.Path) > 6 {
var err error
queriedID, err = NewAddressFromString(req.URL.Path[6:])
if err != nil {
_ = apiSendObj(out, req, http.StatusNotFound, nil)
return
}
queriedName = req.URL.Path[6:]
}
if req.Method == http.MethodPost || req.Method == http.MethodPut {
if queriedID == 0 {
_ = apiSendObj(out, req, http.StatusBadRequest, nil)
} else {
var r Root
if apiReadObj(out, req, &r) == nil {
var r Root
if apiReadObj(out, req, &r) == nil {
if r.Name != queriedName && (r.Locator == nil || r.Locator.Identity == nil || r.Locator.Identity.address.String() != queriedName) {
_ = apiSendObj(out, req, http.StatusBadRequest, nil)
}
err := node.SetRoot(r.Name, r.Locator)
if err != nil {
_ = apiSendObj(out, req, http.StatusBadRequest, nil)
} else {
roots := node.Roots()
for _, r := range roots {
if r.Name == queriedName {
_ = apiSendObj(out, req, http.StatusOK, r)
return
}
}
_ = apiSendObj(out, req, http.StatusNotFound, nil)
}
}
} else if req.Method == http.MethodGet || req.Method == http.MethodHead {
roots := node.Roots()
_ = apiSendObj(out, req, http.StatusOK, roots)
for _, r := range roots {
if r.Name == queriedName {
_ = apiSendObj(out, req, http.StatusOK, r)
return
}
}
_ = apiSendObj(out, req, http.StatusNotFound, nil)
} else {
out.Header().Set("Allow", "GET, HEAD, PUT, POST")
_ = apiSendObj(out, req, http.StatusMethodNotAllowed, nil)

View file

@ -42,7 +42,9 @@ func NewLocatorDNSSigningKey() (*LocatorDNSSigningKey, error) {
return &sk, nil
}
// Locator is a binary serialized record containing information about where a ZeroTier node is located on the network
// Locator is a binary serialized record containing information about where a ZeroTier node is located on the network.
// Note that for JSON objects only Bytes needs to be specified. When JSON is deserialized only this field is used
// and the others are always reconstructed from it.
type Locator struct {
// Identity is the full identity of the node being located
Identity *Identity
@ -53,7 +55,8 @@ type Locator struct {
// Virtual is a list of ZeroTier nodes that can relay to this node
Virtual []*Identity
bytes []byte
// Bytes is the raw serialized Locator
Bytes []byte
}
// NewLocator creates a new locator with the given identity and addresses and the current time as timestamp.
@ -108,7 +111,7 @@ func NewLocator(id *Identity, virtualAddresses []*Identity, physicalAddresses []
Identity: id,
Physical: physicalAddresses,
Virtual: virtualAddresses,
bytes: r,
Bytes: r,
}, nil
}
@ -148,26 +151,23 @@ func NewLocatorFromBytes(b []byte) (*Locator, error) {
return &loc, nil
}
// Bytes returns this locator in byte serialized format
func (l *Locator) Bytes() []byte { return l.bytes }
// MarshalJSON marshals this Locator as its byte encoding
func (l *Locator) MarshalJSON() ([]byte, error) {
b := l.bytes
return json.Marshal(&b)
return json.Marshal(l)
}
// UnmarshalJSON unmarshals this Locator from a byte array in JSON.
func (l *Locator) UnmarshalJSON(j []byte) error {
var ba []byte
err := json.Unmarshal(j, &ba)
if err != nil {
return nil
}
tmp, err := NewLocatorFromBytes(ba)
err := json.Unmarshal(j, l)
if err != nil {
return err
}
*l = *tmp
tmp, err := NewLocatorFromBytes(l.Bytes)
if err != nil {
return err
}
l.Identity = tmp.Identity
l.Physical = tmp.Physical
l.Virtual = tmp.Virtual
return nil
}

View file

@ -16,8 +16,6 @@ package zerotier
// Root describes a root server used to find and establish communication with other nodes.
type Root struct {
Name string
Identity *Identity
Addresses []InetAddress
Locator *Locator
Preferred bool
Online bool