5 Commits

4 changed files with 20 additions and 9 deletions

View File

@@ -5,7 +5,7 @@ NAME = apiary
INSTALL_PREFIX ?= /usr/local
VERSION = $(shell cat version.go |grep "Version"| cut -d "=" -f2| tr -d "\" ")
VERSION = $(shell cat version.go |grep "var Version"| cut -d "=" -f2| tr -d "\" ")
ARCH = $(shell go env | grep GOHOSTARCH | cut -d"=" -f2 | tr -d "\"")
OS = $(shell go env | grep GOHOSTOS | cut -d"=" -f2 | tr -d "\"")
GIT_COMMIT := $(shell git rev-parse --short HEAD)

View File

@@ -3,6 +3,7 @@ package store
import (
"database/sql"
"fmt"
"net"
_ "github.com/jackc/pgx/v4/stdlib"
"github.uio.no/torjus/apiary/models"
@@ -161,6 +162,7 @@ func (s *PostgresStore) statsTotal(limit int) ([]StatsResult, error) {
func (s *PostgresStore) Query(query AttemptQuery) ([]models.LoginAttempt, error) {
var stmt string
queryString := query.Query
switch query.QueryType {
case AttemptQueryTypeIP:
@@ -168,15 +170,17 @@ func (s *PostgresStore) Query(query AttemptQuery) ([]models.LoginAttempt, error)
FROM login_attempts WHERE remote_ip = $1`
case AttemptQueryTypePassword:
stmt = `SELECT id, date, remote_ip, username, password, client_version, connection_uuid, country
FROM login_attempts WHERE password like '%$1%'`
FROM login_attempts WHERE password like $1`
queryString = fmt.Sprintf("%%%s%%", queryString)
case AttemptQueryTypeUsername:
stmt = `SELECT id, date, remote_ip, username, password, client_version, connection_uuid, country
FROM login_attempts WHERE username like '%$1%'`
FROM login_attempts WHERE username like $1`
queryString = fmt.Sprintf("%%%s%%", queryString)
default:
return nil, fmt.Errorf("Invalid query type")
}
rows, err := s.db.Query(stmt, query.Query)
rows, err := s.db.Query(stmt, queryString)
if err != nil {
return nil, fmt.Errorf("Unable to query database: %w", err)
}
@@ -185,9 +189,11 @@ func (s *PostgresStore) Query(query AttemptQuery) ([]models.LoginAttempt, error)
var results []models.LoginAttempt
for rows.Next() {
var la models.LoginAttempt
if err := rows.Scan(&la.ID, &la.Date, &la.RemoteIP, &la.Username, &la.Password, &la.SSHClientVersion, &la.ConnectionUUID, &la.Country); err != nil {
var ipString string
if err := rows.Scan(&la.ID, &la.Date, &ipString, &la.Username, &la.Password, &la.SSHClientVersion, &la.ConnectionUUID, &la.Country); err != nil {
return nil, fmt.Errorf("Unable to unmarshal data from database: %w", err)
}
la.RemoteIP = net.ParseIP(ipString)
results = append(results, la)
}

View File

@@ -1,14 +1,17 @@
package apiary
import "fmt"
import (
"fmt"
"runtime"
)
var Version = "v0.1.2"
var Version = "v0.1.4"
var Build string
func FullVersion() string {
if Build != "" {
return fmt.Sprintf("%s-%s", Version, Build)
return fmt.Sprintf("%s-%s (%s)", Version, Build, runtime.Version())
}
return Version
return fmt.Sprintf("%s (%s)", Version, runtime.Version())
}

View File

@@ -14,6 +14,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/google/uuid"
"github.uio.no/torjus/apiary"
"github.uio.no/torjus/apiary/config"
"github.uio.no/torjus/apiary/honeypot"
"github.uio.no/torjus/apiary/honeypot/store"
@@ -151,6 +152,7 @@ func (s *Server) HandlerAttemptStream(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Server", apiary.FullVersion())
id, ch := s.addAttemptListener()
defer s.closeAttemptListener(id)
w.WriteHeader(http.StatusOK)