Skip to content

Commit

Permalink
Added SSH/SFTP configuration support
Browse files Browse the repository at this point in the history
  • Loading branch information
telmomarques committed Jun 12, 2020
1 parent c487f2d commit 02fbf14
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 50 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
web-config-server
web-config-server
.vscode
bin
pkg
11 changes: 10 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
Expand Down Expand Up @@ -60,6 +61,11 @@ func Save(hackID string, configStruct interface{}) bool {
return true
}

func Load(hackID string, configStruct interface{}) {
file, _ := ioutil.ReadFile(GetMetaConfigFilePathForHack(hackID))
json.Unmarshal([]byte(file), configStruct)
}

func writeMetaConfigFile(hackID string, configStruct interface{}) bool {
var json, _ = json.MarshalIndent(configStruct, "", " ")
writeFileError := ioutil.WriteFile(GetMetaConfigFilePathForHack(hackID), json, 0644)
Expand All @@ -79,18 +85,21 @@ func writeConfigFile(hackID string, configStruct interface{}) bool {
parsedTemplate, parseTemplateError := template.New("configTemplate").Parse(string(templateContent))

if parseTemplateError != nil {
fmt.Println(parseTemplateError)
return false
}

configFile, openFileError := os.OpenFile(GetConfigFilePathForHackAndTemplate(hackID, templateFile), os.O_RDWR|os.O_CREATE, 0644)
configFile, openFileError := os.OpenFile(GetConfigFilePathForHackAndTemplate(hackID, templateFile), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)

if openFileError != nil {
fmt.Println(openFileError)
return false
}

executeError := parsedTemplate.Execute(configFile, configStruct)

if executeError != nil {
fmt.Println(executeError)
return false
}

Expand Down
13 changes: 13 additions & 0 deletions customerror/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package customerror

import "fmt"

type Error struct {
Code int `json:"code"`
HTTPCode int
Message string `json:"message"`
}

func (e *Error) Error() string {
return fmt.Sprintf("[Error] code: %d, httpCode: %d, message: %s", e.Code, e.HTTPCode, e.Message)
}
145 changes: 145 additions & 0 deletions hack/sshserver/sshserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package sshserver

import (
"github.com/kless/osutil/user/crypt/sha512_crypt"
"github.com/telmomarques/x360h1080p-web-config-server/config"
"github.com/telmomarques/x360h1080p-web-config-server/customerror"
"github.com/telmomarques/x360h1080p-web-config-server/service"
)

const ID = "ssh-server"
const FriendlyName = "SSH / SFTP Server"

const serviceName = "ssh-server-ssh-server"

type SSHUser struct {
SystemUsername string `json:"systemUsername"`
Username string `json:"username"`
Password string `json:"password"`
}

type SSHServerConfig struct {
Enable bool `json:"enable"`
Users []SSHUser `json:"users"`
}

type SSHGeneralConfig struct {
Enable bool `json:"enable"`
}

type SSHUserConfig struct {
Users []SSHUser `json:"users"`
}

func GetGeneralConfiguration() SSHGeneralConfig {
var currentConfig SSHServerConfig
var generalConfig SSHGeneralConfig

config.Load(ID, &currentConfig)

generalConfig.Enable = currentConfig.Enable

return generalConfig
}

func GetUserConfiguration() SSHUserConfig {
var currentConfig SSHServerConfig
var userConfig SSHUserConfig

config.Load(ID, &currentConfig)

userConfig.Users = currentConfig.Users

return userConfig
}

func SaveGeneralConfig(newConfig SSHGeneralConfig) bool {
var updatedconfig SSHServerConfig

config.Load(ID, &updatedconfig)

updatedconfig.Enable = newConfig.Enable

success := config.Save(ID, updatedconfig)

if !success {
return false
}

if updatedconfig.Enable {
config.EnableService(ID)
service.Restart(service.Runit, serviceName)
} else {
config.DisableService(ID)
service.Stop(service.Runit, serviceName)
}

return true
}

func AddUser(user SSHUser) error {
var sshConfig SSHServerConfig

if user.Username == "" {
return &customerror.Error{-2, 400, "Username is required."}
}

if userExists(user.Username) {
return &customerror.Error{-3, 400, "Username already exists."}
}

if user.Password != "" {
crypter := sha512_crypt.New()
salter := sha512_crypt.GetSalt()
salt := salter.Generate(salter.SaltLenMax)
passwordHash, _ := crypter.Generate([]byte(user.Password), []byte(salt))
user.Password = passwordHash
}

config.Load(ID, &sshConfig)

sshConfig.Users = append(sshConfig.Users, user)

success := config.Save(ID, sshConfig)

if !success {
return &customerror.Error{-2, 500, "Error adding user."}
}

return nil
}

func DeleteUser(username string) bool {
var sshConfig SSHServerConfig

config.Load(ID, &sshConfig)

for i, user := range sshConfig.Users {
if username == user.Username {
sshConfig.Users = append(sshConfig.Users[:i], sshConfig.Users[i+1:]...)
break
}
}

success := config.Save(ID, sshConfig)

if !success {
return false
}

return true
}

func userExists(username string) bool {
var sshConfig SSHServerConfig

config.Load(ID, &sshConfig)

for _, user := range sshConfig.Users {
if username == user.Username {
return true
}
}

return false
}
Loading

0 comments on commit 02fbf14

Please sign in to comment.