-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SSH/SFTP configuration support
- Loading branch information
1 parent
c487f2d
commit 02fbf14
Showing
5 changed files
with
269 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
web-config-server | ||
web-config-server | ||
.vscode | ||
bin | ||
pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ¤tConfig) | ||
|
||
generalConfig.Enable = currentConfig.Enable | ||
|
||
return generalConfig | ||
} | ||
|
||
func GetUserConfiguration() SSHUserConfig { | ||
var currentConfig SSHServerConfig | ||
var userConfig SSHUserConfig | ||
|
||
config.Load(ID, ¤tConfig) | ||
|
||
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 | ||
} |
Oops, something went wrong.