Skip to content

Commit

Permalink
merge gitserver branch
Browse files Browse the repository at this point in the history
  • Loading branch information
hugefiver committed Oct 2, 2022
2 parents 065e50c + a0853fb commit df61e94
Show file tree
Hide file tree
Showing 29 changed files with 844 additions and 116 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"files.eol": "\n"
"files.eol": "\n",
"go.toolsEnvVars": {
"GOOS": "linux",
}
}
152 changes: 152 additions & 0 deletions conf/conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package conf

import (
"io"
"os"

"github.com/hugefiver/fakessh/modules/gitserver"
"github.com/pelletier/go-toml/v2"
)

type AppConfig struct {
BaseConfig

Modules ModulesConfig `toml:"modules"`
}

type BaseConfig struct {
Server struct {
ServPort string `toml:"bind"`
SSHVersion string `toml:"version"`

MaxTry int `toml:"max_try"`
Delay int `toml:"delay"`
Deviation int `toml:"deviation"`

AntiScan bool `toml:"anti_scan"`
} `toml:"server"`

Log struct {
LogFile string `toml:"file"`
LogLevel string `toml:"level"`
LogFormat string `toml:"format"`
IsLogPasswd bool `toml:"log_passwd"`
} `toml:"log"`

Key struct {
KeyFiles []string `toml:"key"`
KeyType string `toml:"type"`
} `toml:"key"`
}

type ModulesConfig struct {
GitServer gitserver.Config `toml:"gitserver"`
}

func (c *BaseConfig) FillDefault() error {
c.Server.ServPort = DefaultBind
c.Server.SSHVersion = DefaultSSHVersion
c.Server.Delay = DefaultDelay
c.Server.Deviation = DefaultDeviation
c.Server.AntiScan = DefaultEnableAntiScan

c.Log.LogLevel = DefaultLogLevel
c.Log.LogFormat = DefaultLogFormat
c.Log.IsLogPasswd = false

c.Key.KeyType = DefaultKeyType

return nil
}

// func (c *AppConfig) FillDefault() error {
// if err := c.BaseConfig.FillDefault(); err != nil {
// return err
// }

// if err := c.Modules.GitServer.FillDefault(); err != nil {
// return err
// }
// return nil
// }

func NewDefaultAppConfig() *AppConfig {
c := &AppConfig{}

c.BaseConfig.FillDefault()

return c
}

func ParseConfig(s []byte) (*AppConfig, error) {
var config AppConfig
config.FillDefault()

if err := toml.Unmarshal(s, &config); err != nil {
return nil, err
}

// Fill default values of Modules.GitServer
if err := config.Modules.GitServer.FillDefault(); err != nil {
return nil, err
}

return &config, nil
}

func LoadFromFile(file string) (*AppConfig, error) {
r, err := os.Open(file)
if err != nil {
return nil, err
}
defer r.Close()

s, err := io.ReadAll(r)
if err != nil {
return nil, err
}
return ParseConfig(s)
}

func MergeConfig(c *AppConfig, f *FlagArgsStruct, set StringSet) error {
var enableAnti, disableAnti bool

set.ForEach(func(s string) error {
switch s {
case FlagBind:
c.Server.ServPort = f.ServPort
case FlagSSHVersion:
c.Server.SSHVersion = f.SSHVersion
case FlagMaxTry:
c.Server.MaxTry = f.MaxTry
case FlagDelay:
c.Server.Delay = f.Delay
case FlagDeviation:
c.Server.Deviation = f.Deviation

case FlagLogFile:
c.Log.LogFile = f.LogFile
case FlagLogLevel:
c.Log.LogLevel = f.LogLevel
case FlagLogFormat:
c.Log.LogFormat = f.LogFormat
case FlagLogPasswd:
c.Log.IsLogPasswd = f.IsLogPasswd

case FlagKeyPaths:
c.Key.KeyFiles = f.KeyFiles
case FlagKeyType:
c.Key.KeyType = f.KeyType
case FlagEnableAntiScan:
enableAnti = true
case FlagDisableAntiScan:
disableAnti = true
}
return nil
})

if enableAnti || disableAnti {
c.Server.AntiScan = enableAnti
}
return nil
}
91 changes: 91 additions & 0 deletions conf/conf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package conf

import (
"testing"

"github.com/hugefiver/fakessh/modules/gitserver"
"github.com/stretchr/testify/assert"
)

const c1 = `
[modules.gitserver]
enable = false
#user = "git"
#current_user = false
#ssh_user = "git"
#git_shell = "/usr/bin/git-shell"
#git_user_home = "/home/git"
#authorized_keys = "/home/git/.ssh/authorized_keys"
#watch_keys = false
`

const c2 = `
[modules.gitserver]
enable = true
#user = "git"
current_user = true
#ssh_user = "git"
#git_shell = "/usr/bin/git-shell"
#git_user_home = "/home/git"
#authorized_keys = "/home/git/.ssh/authorized_keys"
#watch_keys = false
`

const c3 = `
[modules.gitserver]
enable = true
user = "git"
current_user = false
ssh_user = "git"
git_shell = "/usr/bin/git-shell"
git_user_home = "/home/git"
authorized_keys = "/home/git/.ssh/authorized_keys"
watch_keys = true
`

func TestParseConfig(t *testing.T) {
t.Run("test_gitserver_1", func(t *testing.T) {
c, _ := ParseConfig([]byte(c1))
assert.Equal(t, gitserver.Config{
Enable: false,
User: "git",
CurrentUser: false,
SSHUser: "git",
GitShell: "git-shell",
GitUserHome: "/home/git",
AuthorizedKeys: "/home/git/.ssh/authorized_keys",
WatchKeys: false,
}, c.Modules.GitServer)
})

t.Run("test_gitserver_2", func(t *testing.T) {
c, _ := ParseConfig([]byte(c2))
assert.Equal(t, gitserver.Config{
Enable: true,
User: "git",
CurrentUser: true,
SSHUser: "git",
GitShell: "git-shell",
GitUserHome: "/home/git",
AuthorizedKeys: "/home/git/.ssh/authorized_keys",
WatchKeys: false,
}, c.Modules.GitServer)
})

t.Run("test_gitserver_3", func(t *testing.T) {
c, _ := ParseConfig([]byte(c3))
assert.Equal(t, gitserver.Config{
Enable: true,
User: "git",
CurrentUser: false,
SSHUser: "git",
GitShell: "/usr/bin/git-shell",
GitUserHome: "/home/git",
AuthorizedKeys: "/home/git/.ssh/authorized_keys",
WatchKeys: true,
}, c.Modules.GitServer)
})
}
50 changes: 50 additions & 0 deletions conf/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[server]
# bind = ":22"
# version = "OpenSSH_8.8p1"
# max_try = 3
# delay = 0
# deviation = 0
# anti_scan = true

[log]
# file = ""
# level = "info"
# format = "plain"
# log_password = false

[key]
# key = []
# type = "ed25519" # e.g. "ed25519,rsa:2048,ecdsa:256"

[modules.gitserver]
# enable = false

# user used serve git service
# default: git
#user = "git"

# if true, git-shell will run with current user
# default: false
#current_user = false

# user used to connect from ssh
# default same with field `user`
#ssh_user = "git"

# path of `git-shell` execute file
# empty means search in `PATH`
#git_shell = "/usr/bin/git-shell"

# path to store git repositories
# default: /home/git
#git_user_home = "/home/git"

# path of `authorized_keys` file
# default: {git_user_home}/.ssh/authorized_keys
#authorized_keys = "/home/git/.ssh/authorized_keys"

# if true, authorized_keys will be auto updated
#watch_keys = false

max_git_shell_processes = 0
refuse_when_busy = false
19 changes: 19 additions & 0 deletions conf/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package conf

const (
DefaultBind = ":22"
DefaultSSHVersion = "OpenSSH_8.8p1"

DefaultLogLevel = "info"
DefaultLogFormat = "plain"
DefaultLogPassword = false

DefaultKeyType = "ed25519"

DefaultMaxTry = 3

DefaultDelay = 0
DefaultDeviation = 0

DefaultEnableAntiScan = true
)
Loading

0 comments on commit df61e94

Please sign in to comment.