Compare commits

...

2 commits

2 changed files with 8 additions and 35 deletions

20
main.go
View file

@ -6,11 +6,9 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"strings"
"sync"
"time"
@ -89,7 +87,7 @@ func startAccepting(listener net.Listener, serverConfig ssh.ServerConfig) {
func getKey(path string) []byte {
if path != "" {
privateBytes, err := ioutil.ReadFile(path)
privateBytes, err := os.ReadFile(path)
if err != nil {
log.Fatalf("Failed to load private key: %s", path)
}
@ -119,7 +117,12 @@ func connectionHandler(con net.Conn, serverConfig ssh.ServerConfig) {
}
func printConnectionData(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
ip := getIPWithoutPort(conn.RemoteAddr().String())
responseErr := fmt.Errorf("password rejected for %s", conn.User())
ip, _, err := net.SplitHostPort(conn.RemoteAddr().String())
if err != nil {
fmt.Println(err)
return nil, responseErr
}
if isJson {
fmt.Printf(
@ -133,7 +136,7 @@ func printConnectionData(conn ssh.ConnMetadata, password []byte) (*ssh.Permissio
log.Printf("SRC=%s USERNAME=%s PASSWORD=%s\n", ip, conn.User(), string(password))
}
return nil, fmt.Errorf("password rejected for %s", conn.User())
return nil, responseErr
}
func printHelp() {
@ -145,10 +148,3 @@ USAGE:
FLAGS:`)
flags.PrintDefaults()
}
func getIPWithoutPort(address string) string {
if strings.Contains(address, "]") { // Is IPv6
return strings.ReplaceAll(strings.Split(address, "]")[0], "[", "")
}
return strings.Split(address, ":")[0]
}

View file

@ -1,23 +0,0 @@
package main
import "testing"
func TestGetIPWithoutPort(t *testing.T) {
t.Run("IPv6", func(t *testing.T) {
got := getIPWithoutPort("[::1]:2222")
want := "::1"
if want != got {
t.Fatalf("Want '%s' but got '%s'", want, got)
}
})
t.Run("IPv4", func(t *testing.T) {
got := getIPWithoutPort("127.0.0.1:2222")
want := "127.0.0.1"
if want != got {
t.Fatalf("Want '%s' but got '%s'", want, got)
}
})
}