mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-06 10:23:03 +03:00
Removed regex, added timeout.
This commit is contained in:
parent
3c466dc88e
commit
a1455a8eba
25
server.go
25
server.go
@ -293,32 +293,35 @@ func (s *Server) Whitelist(fingerprint string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var pubKeyRegex = regexp.MustCompile(`ssh-rsa ([A-Za-z0-9\+=\/]+)\s*`)
|
// Client for getting github pub keys
|
||||||
|
var timeout = time.Duration(10 * time.Second)
|
||||||
|
var client = http.Client{
|
||||||
|
Timeout: timeout,
|
||||||
|
}
|
||||||
// Returns an array of public keys for the given github user URL
|
// Returns an array of public keys for the given github user URL
|
||||||
func getGithubPubKeys(url string) ([]ssh.PublicKey, error) {
|
func getGithubPubKeys(url string) ([]ssh.PublicKey, error) {
|
||||||
timeout := time.Duration(10 * time.Second)
|
|
||||||
client := http.Client{
|
|
||||||
Timeout: timeout,
|
|
||||||
}
|
|
||||||
resp, err := client.Get("http://" + url + ".keys")
|
resp, err := client.Get("http://" + url + ".keys")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
bodyStr := string(body)
|
bodyStr := string(body)
|
||||||
keys := pubKeyRegex.FindAllStringSubmatch(bodyStr, -1)
|
pubs := []ssh.PublicKey{}
|
||||||
pubs := make([]ssh.PublicKey, 0, 3)
|
for _, key := range strings.SplitN(bodyStr, "\n", -1) {
|
||||||
for _, key := range keys {
|
splitKey := strings.SplitN(key, " ", -1)
|
||||||
if(len(key) < 2) {
|
|
||||||
|
// In case of malformated key
|
||||||
|
if len(splitKey) < 2 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
bodyDecoded, err := base64.StdEncoding.DecodeString(key[1])
|
bodyDecoded, err := base64.StdEncoding.DecodeString(splitKey[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user