fix: make key names deterministic
This commit is contained in:
		
							parent
							
								
									19b0a80f57
								
							
						
					
					
						commit
						59a0e9bb44
					
				
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							@ -7,6 +7,7 @@ require (
 | 
				
			|||||||
	github.com/gliderlabs/ssh v0.3.2
 | 
						github.com/gliderlabs/ssh v0.3.2
 | 
				
			||||||
	github.com/soheilhy/cmux v0.1.5
 | 
						github.com/soheilhy/cmux v0.1.5
 | 
				
			||||||
	github.com/tjarratt/babble v0.0.0-20210505082055-cbca2a4833c1
 | 
						github.com/tjarratt/babble v0.0.0-20210505082055-cbca2a4833c1
 | 
				
			||||||
 | 
						github.com/wolfeidau/humanhash v1.1.0 // indirect
 | 
				
			||||||
	go.uber.org/multierr v1.7.0
 | 
						go.uber.org/multierr v1.7.0
 | 
				
			||||||
	golang.org/dl v0.0.0-20210816190658-eea66df5a73d // indirect
 | 
						golang.org/dl v0.0.0-20210816190658-eea66df5a73d // indirect
 | 
				
			||||||
	golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf // indirect
 | 
						golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf // indirect
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							@ -12,6 +12,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
 | 
				
			|||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 | 
					github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 | 
				
			||||||
github.com/tjarratt/babble v0.0.0-20210505082055-cbca2a4833c1 h1:j8whCiEmvLCXI3scVn+YnklCU8mwJ9ZJ4/DGAKqQbRE=
 | 
					github.com/tjarratt/babble v0.0.0-20210505082055-cbca2a4833c1 h1:j8whCiEmvLCXI3scVn+YnklCU8mwJ9ZJ4/DGAKqQbRE=
 | 
				
			||||||
github.com/tjarratt/babble v0.0.0-20210505082055-cbca2a4833c1/go.mod h1:O5hBrCGqzfb+8WyY8ico2AyQau7XQwAfEQeEQ5/5V9E=
 | 
					github.com/tjarratt/babble v0.0.0-20210505082055-cbca2a4833c1/go.mod h1:O5hBrCGqzfb+8WyY8ico2AyQau7XQwAfEQeEQ5/5V9E=
 | 
				
			||||||
 | 
					github.com/wolfeidau/humanhash v1.1.0 h1:06KgtyyABJGBbrfMONrW7S+b5TTYVyrNB/jss5n7F3E=
 | 
				
			||||||
 | 
					github.com/wolfeidau/humanhash v1.1.0/go.mod h1:jkpynR1bfyfkmKEQudIC0osWKynFAoayRjzH9OJdVIg=
 | 
				
			||||||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
 | 
					go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
 | 
				
			||||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
 | 
					go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
 | 
				
			||||||
go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=
 | 
					go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										23
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								main.go
									
									
									
									
									
								
							@ -2,6 +2,7 @@ package main
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"crypto/sha256"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io/ioutil"
 | 
						"io/ioutil"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
@ -19,7 +20,7 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	"github.com/gliderlabs/ssh"
 | 
						"github.com/gliderlabs/ssh"
 | 
				
			||||||
	"github.com/soheilhy/cmux"
 | 
						"github.com/soheilhy/cmux"
 | 
				
			||||||
	"github.com/tjarratt/babble"
 | 
						"github.com/wolfeidau/humanhash"
 | 
				
			||||||
	"go.uber.org/multierr"
 | 
						"go.uber.org/multierr"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -153,18 +154,20 @@ type user struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (srv *server) AddUser(pubkey ssh.PublicKey) *user {
 | 
					func (srv *server) AddUser(pubkey ssh.PublicKey) *user {
 | 
				
			||||||
	u := &user{}
 | 
						u := &user{}
 | 
				
			||||||
	u.pubkey = pubkey
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	babbler := babble.NewBabbler()
 | 
						u.name = fingerprintHuman(pubkey)
 | 
				
			||||||
	u.name = babbler.Babble()
 | 
					 | 
				
			||||||
	u.name = strings.ToLower(u.name)
 | 
						u.name = strings.ToLower(u.name)
 | 
				
			||||||
	u.name = filterName.ReplaceAllString(u.name, "")
 | 
						u.name = filterName.ReplaceAllString(u.name, "")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if g, ok := srv.users.LoadOrStore(u.name, u); ok {
 | 
				
			||||||
 | 
							u = g.(*user)
 | 
				
			||||||
 | 
							return u
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						u.pubkey = pubkey
 | 
				
			||||||
	u.bindPort = srv.nextPort()
 | 
						u.bindPort = srv.nextPort()
 | 
				
			||||||
	u.bindHost = srv.bindHost
 | 
						u.bindHost = srv.bindHost
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	srv.users.Store(u.name, u)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return u
 | 
						return u
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
func (srv *server) nextPort() uint32 {
 | 
					func (srv *server) nextPort() uint32 {
 | 
				
			||||||
@ -310,7 +313,7 @@ func (srv *server) handleHTTP(rw http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if r.Method == http.MethodPost {
 | 
						if r.Method == http.MethodPost {
 | 
				
			||||||
		pubkey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(r.PostFormValue("pub")))
 | 
							pubkey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(r.FormValue("pub")))
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			rw.WriteHeader(400)
 | 
								rw.WriteHeader(400)
 | 
				
			||||||
			fmt.Fprintln(rw, "ERR READING KEY")
 | 
								fmt.Fprintln(rw, "ERR READING KEY")
 | 
				
			||||||
@ -423,3 +426,9 @@ func (m *serverMux) Serve(ctx context.Context) error {
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func fingerprintHuman(pubKey ssh.PublicKey) string {
 | 
				
			||||||
 | 
						sha256sum := sha256.Sum256(pubKey.Marshal())
 | 
				
			||||||
 | 
						h, _ := humanhash.Humanize(sha256sum[:], 3)
 | 
				
			||||||
 | 
						return h
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user