feat: added the shutil to make hashes.
This commit is contained in:
parent
f850ef3061
commit
685282c13c
10 changed files with 118 additions and 21 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/exe/
|
|
@ -1,3 +1,3 @@
|
|||
#!/bin/sh
|
||||
|
||||
go build ./cmd/...
|
||||
go build -o ./exe ./cmd/...
|
||||
|
|
|
@ -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
36
cmd/shutil/main.go
Normal 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
5
go.mod
|
@ -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
4
go.sum
Normal 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=
|
|
@ -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
20
servers/config.go
Normal 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
24
servers/hash.go
Normal 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
|
||||
}
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue