diff options
author | Jakob Borg <jakob@nym.se> | 2014-06-30 01:42:03 +0200 |
---|---|---|
committer | Jakob Borg <jakob@nym.se> | 2014-06-30 01:42:03 +0200 |
commit | 8f3effed32bce654110273a78759d659cccc86c2 (patch) | |
tree | ef15f8c46b69a7618a1976a48194d7e61c82c5a9 /cmd | |
parent | fee8289c0ac62b88fa77423446faa882f7132796 (diff) |
Refactor node ID handling, use check digits (fixes #269)
New node ID:s contain four Luhn check digits and are grouped
differently. Code uses NodeID type instead of string, so it's formatted
homogenously everywhere.
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/stcli/main.go | 16 | ||||
-rw-r--r-- | cmd/stcli/tls.go | 14 | ||||
-rw-r--r-- | cmd/syncthing/gui.go | 2 | ||||
-rw-r--r-- | cmd/syncthing/main.go | 12 | ||||
-rw-r--r-- | cmd/syncthing/tls.go | 13 | ||||
-rw-r--r-- | cmd/syncthing/usage_report.go | 2 |
6 files changed, 22 insertions, 37 deletions
diff --git a/cmd/stcli/main.go b/cmd/stcli/main.go index 161efae84d..ee6d19bc3c 100644 --- a/cmd/stcli/main.go +++ b/cmd/stcli/main.go @@ -46,12 +46,12 @@ func connect(target string) { log.Fatal(err) } - myID := string(certID(cert.Certificate[0])) + myID := protocol.NewNodeID(cert.Certificate[0]) tlsCfg := &tls.Config{ Certificates: []tls.Certificate{cert}, NextProtos: []string{"bep/1.0"}, - ServerName: myID, + ServerName: myID.String(), ClientAuth: tls.RequestClientCert, SessionTicketsDisabled: true, InsecureSkipVerify: true, @@ -63,7 +63,7 @@ func connect(target string) { log.Fatal(err) } - remoteID := certID(conn.ConnectionState().PeerCertificates[0].Raw) + remoteID := protocol.NewNodeID(conn.ConnectionState().PeerCertificates[0].Raw) pc = protocol.NewConnection(remoteID, conn, conn, Model{}) @@ -82,7 +82,7 @@ func prtIndex(files []protocol.FileInfo) { } } -func (m Model) Index(nodeID string, repo string, files []protocol.FileInfo) { +func (m Model) Index(nodeID protocol.NodeID, repo string, files []protocol.FileInfo) { log.Printf("Received index for repo %q", repo) if cmd == "idx" { prtIndex(files) @@ -121,7 +121,7 @@ func getFile(f protocol.FileInfo) { fd.Close() } -func (m Model) IndexUpdate(nodeID string, repo string, files []protocol.FileInfo) { +func (m Model) IndexUpdate(nodeID protocol.NodeID, repo string, files []protocol.FileInfo) { log.Printf("Received index update for repo %q", repo) if cmd == "idx" { prtIndex(files) @@ -131,16 +131,16 @@ func (m Model) IndexUpdate(nodeID string, repo string, files []protocol.FileInfo } } -func (m Model) ClusterConfig(nodeID string, config protocol.ClusterConfigMessage) { +func (m Model) ClusterConfig(nodeID protocol.NodeID, config protocol.ClusterConfigMessage) { log.Println("Received cluster config") log.Printf("%#v", config) } -func (m Model) Request(nodeID, repo string, name string, offset int64, size int) ([]byte, error) { +func (m Model) Request(nodeID protocol.NodeID, repo string, name string, offset int64, size int) ([]byte, error) { log.Println("Received request") return nil, io.EOF } -func (m Model) Close(nodeID string, err error) { +func (m Model) Close(nodeID protocol.NodeID, err error) { log.Println("Received close") } diff --git a/cmd/stcli/tls.go b/cmd/stcli/tls.go index 1d89f85899..2be011238e 100644 --- a/cmd/stcli/tls.go +++ b/cmd/stcli/tls.go @@ -5,20 +5,12 @@ package main import ( - "crypto/sha256" "crypto/tls" - "encoding/base32" "path/filepath" - "strings" ) func loadCert(dir string) (tls.Certificate, error) { - return tls.LoadX509KeyPair(filepath.Join(dir, "cert.pem"), filepath.Join(dir, "key.pem")) -} - -func certID(bs []byte) string { - hf := sha256.New() - hf.Write(bs) - id := hf.Sum(nil) - return strings.Trim(base32.StdEncoding.EncodeToString(id), "=") + cf := filepath.Join(dir, "cert.pem") + kf := filepath.Join(dir, "key.pem") + return tls.LoadX509KeyPair(cf, kf) } diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index 5d70ed5804..d000ff1615 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -327,7 +327,7 @@ func restGetSystem(w http.ResponseWriter) { runtime.ReadMemStats(&m) res := make(map[string]interface{}) - res["myID"] = myID + res["myID"] = myID.String() res["goroutines"] = runtime.NumGoroutine() res["alloc"] = m.Alloc res["sys"] = m.Sys diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index dfbb4957de..90ed07226a 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -61,7 +61,7 @@ func init() { var ( cfg config.Configuration - myID string + myID protocol.NodeID confDir string logFlags int = log.Ltime rateBucket *ratelimit.Bucket @@ -181,8 +181,8 @@ func main() { l.FatalErr(err) } - myID = certID(cert.Certificate[0]) - l.SetPrefix(fmt.Sprintf("[%s] ", myID[:5])) + myID = protocol.NewNodeID(cert.Certificate[0]) + l.SetPrefix(fmt.Sprintf("[%s] ", myID.String()[:5])) l.Infoln(LongVersion) l.Infoln("My ID:", myID) @@ -263,7 +263,7 @@ func main() { tlsCfg := &tls.Config{ Certificates: []tls.Certificate{cert}, NextProtos: []string{"bep/1.0"}, - ServerName: myID, + ServerName: myID.String(), ClientAuth: tls.RequestClientCert, SessionTicketsDisabled: true, InsecureSkipVerify: true, @@ -567,7 +567,7 @@ func saveConfig() { saveConfigCh <- struct{}{} } -func listenConnect(myID string, m *model.Model, tlsCfg *tls.Config) { +func listenConnect(myID protocol.NodeID, m *model.Model, tlsCfg *tls.Config) { var conns = make(chan *tls.Conn) // Listen @@ -673,7 +673,7 @@ next: conn.Close() continue } - remoteID := certID(certs[0].Raw) + remoteID := protocol.NewNodeID(certs[0].Raw) if remoteID == myID { l.Infof("Connected to myself (%s) - should not happen", remoteID) diff --git a/cmd/syncthing/tls.go b/cmd/syncthing/tls.go index 51231cf13e..e9f989616c 100644 --- a/cmd/syncthing/tls.go +++ b/cmd/syncthing/tls.go @@ -11,14 +11,12 @@ import ( "crypto/tls" "crypto/x509" "crypto/x509/pkix" - "encoding/base32" "encoding/binary" "encoding/pem" "math/big" mr "math/rand" "os" "path/filepath" - "strings" "time" ) @@ -28,14 +26,9 @@ const ( ) func loadCert(dir string, prefix string) (tls.Certificate, error) { - return tls.LoadX509KeyPair(filepath.Join(dir, prefix+"cert.pem"), filepath.Join(dir, prefix+"key.pem")) -} - -func certID(bs []byte) string { - hf := sha256.New() - hf.Write(bs) - id := hf.Sum(nil) - return strings.Trim(base32.StdEncoding.EncodeToString(id), "=") + cf := filepath.Join(dir, prefix+"cert.pem") + kf := filepath.Join(dir, prefix+"key.pem") + return tls.LoadX509KeyPair(cf, kf) } func certSeed(bs []byte) int64 { diff --git a/cmd/syncthing/usage_report.go b/cmd/syncthing/usage_report.go index cee7196aab..46bd9a3e3c 100644 --- a/cmd/syncthing/usage_report.go +++ b/cmd/syncthing/usage_report.go @@ -23,7 +23,7 @@ var stopUsageReportingCh = make(chan struct{}) func reportData(m *model.Model) map[string]interface{} { res := make(map[string]interface{}) - res["uniqueID"] = strings.ToLower(certID([]byte(myID)))[:6] + res["uniqueID"] = strings.ToLower(myID.String()[:6]) res["version"] = Version res["longVersion"] = LongVersion res["platform"] = runtime.GOOS + "-" + runtime.GOARCH |