mirror of
https://git.sr.ht/~hamburghammer/sshlog
synced 2024-05-13 14:44:37 +02:00
Compare commits
2 commits
fd3e5530f9
...
b5c2560358
Author | SHA1 | Date | |
---|---|---|---|
Augusto Dwenger J. | b5c2560358 | ||
Augusto Dwenger J. | 61fc3ad141 |
20
main.go
20
main.go
|
@ -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]
|
||||
}
|
||||
|
|
23
main_test.go
23
main_test.go
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue