feat: added the shutil to make hashes.

This commit is contained in:
Andrey Parhomenko 2024-09-28 21:26:08 +05:00
parent f850ef3061
commit 685282c13c
10 changed files with 118 additions and 21 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/exe/

View file

@ -1,3 +1,3 @@
#!/bin/sh
go build ./cmd/...
go build -o ./exe ./cmd/...

View file

@ -22,11 +22,11 @@ func main() {
os.Exit(1)
}
server, err := servers.NewServer(addr)
server, err := servers.NewServer(servers.ServerConfig{})
if err != nil {
log.Fatalf("servers.New(...): %s\n", err)
}
log.Printf("Trying to serve at %q\n", addr)
server.ListenAndServe()
server.ListenAndServe(addr)
}

36
cmd/shutil/main.go Normal file
View file

@ -0,0 +1,36 @@
package main
import (
"log"
"os"
"surdeus.su/core/cli/mtool"
"surdeus.su/util/shelldoor/servers"
)
var HashTool = mtool.T("hash").
Func(func(flags *mtool.Flags) {
passwords := flags.Parse()
if len(passwords) < 1 {
flags.Usage()
os.Exit(1)
}
for _, password := range passwords {
hash, err := servers.HashPassword(
servers.Password(password),
)
if err != nil {
log.Printf("HashPassword(...): %s\n", err)
continue
}
log.Printf("%s\n", hash)
}
})
var Tool = mtool.T("shutil").Subs(
HashTool,
)
func main() {
Tool.Run(os.Args[1:])
}

5
go.mod
View file

@ -1,3 +1,8 @@
module surdeus.su/util/shelldoor
go 1.22.7
require (
golang.org/x/crypto v0.27.0
surdeus.su/core/cli v0.8.0
)

4
go.sum Normal file
View file

@ -0,0 +1,4 @@
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
surdeus.su/core/cli v0.8.0 h1:HGmRqWl6W5DxFX1nVkUpS+cK0BgYYJdYrZY3VTq3C4k=
surdeus.su/core/cli v0.8.0/go.mod h1:r9JtQz3aEJzpYzMaNUNQHJoYkoWKNPi047qhd5uGlmA=

View file

@ -2,10 +2,19 @@ package servers
import "net"
// The type represents authentication request
// from clients.
type ClientAuthRequest struct {
Login Login
Password Password
}
// The type represents the controlling client.
type Client struct {
conn net.Conn
}
// Returns the new client on the specified connection.
func NewClient(conn net.Conn) *Client {
ret := &Client{}
ret.conn = conn

20
servers/config.go Normal file
View file

@ -0,0 +1,20 @@
package servers
// The type represents unique bot authentication
// token which is needed to connect to the bot net.
type BotAuthToken int64
// More or less secure storage for passwords
// identifying them by login.
type PasswordMap map[Login]PasswordHash
// The type represents sever configuration.
type ServerConfig struct {
// The map stores clients that can login
// and the credentials.
Auth PasswordMap `json:"auth"`
// The token must be unique so only
// needed bots can join the bot net.
BotAuthToken BotAuthToken `json:"bot_auth_token"`
}

24
servers/hash.go Normal file
View file

@ -0,0 +1,24 @@
package servers
import "golang.org/x/crypto/bcrypt"
// The type represents login used to login.
type Login string
// The type represents passwords used to login.
type Password string
// The type represents hash of the password.
type PasswordHash string
// Simple hashing password function.
func HashPassword(password Password) (PasswordHash, error) {
bts, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return PasswordHash(bts), err
}
// Check for password to match the hash.
func PasswordMatchHash(password Password, hash PasswordHash) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}

View file

@ -5,43 +5,41 @@ import (
"net"
)
type BotRequest struct {
}
type BotResponse struct {
}
type ClientRequest struct {
}
type ClientResponse struct {
Error string
Text string
}
// The type represents server that connects
// clients and the bots together.
type Server struct {
addr string
listenner net.Listener
bots []*Bot
clients []*Client
config ServerConfig
}
func NewServer(addr string) (*Server, error) {
// Returns the new server with the specified configuration.
func NewServer(config ServerConfig) (*Server, error) {
srv := &Server{}
srv.addr = addr
srv.config = config
return srv, nil
}
// The method handles the specified connection
// checking out if it is client or a bot.
func (srv *Server) HandleConn(conn net.Conn) {
defer conn.Close()
for {
}
}
func (srv *Server) ListenAndServe() error {
// Start listening and serve both clients and bots.
func (srv *Server) ListenAndServe(addr string) error {
var err error
srv.listenner, err = net.Listen("tcp", srv.addr)
srv.listenner, err = net.Listen("tcp", addr)
if err != nil {
return err
}
srv.addr = addr
for {
conn, err := srv.listenner.Accept()