mirror of
https://github.com/sipeed/NanoKVM.git
synced 2026-09-11 00:22:56 -05:00
create server
This commit is contained in:
35
server/utils/encrypt.go
Normal file
35
server/utils/encrypt.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/mervick/aes-everywhere/go/aes256"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const EncryptSecretKey = "nanokvm-sipeed-2024"
|
||||
|
||||
func Decrypt(ciphertext string) (string, error) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Errorf("decrypt failed: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if ciphertext == "" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
decrypt := aes256.Decrypt(ciphertext, EncryptSecretKey)
|
||||
|
||||
return decrypt, nil
|
||||
}
|
||||
|
||||
func DecodeDecrypt(ciphertext string) (string, error) {
|
||||
decode, err := url.QueryUnescape(ciphertext)
|
||||
if err != nil {
|
||||
log.Errorf("decode ciphertext failed: %s", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
return Decrypt(decode)
|
||||
}
|
||||
42
server/utils/http.go
Normal file
42
server/utils/http.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Download(req *http.Request, target string) error {
|
||||
out, err := os.OpenFile(target, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
|
||||
if err != nil {
|
||||
log.Errorf("create file %s err: %s", target, err)
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
resp, err := (&http.Client{}).Do(req)
|
||||
if err != nil {
|
||||
log.Errorf("download file err: %s", err)
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errors.New("request error")
|
||||
}
|
||||
|
||||
contentType := resp.Header.Get("Content-Type")
|
||||
if contentType != "application/octet-stream" && contentType != "application/zip" {
|
||||
return errors.New("download error")
|
||||
}
|
||||
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
if err != nil {
|
||||
log.Errorf("download file to %s err: %s", target, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
48
server/utils/permission.go
Normal file
48
server/utils/permission.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package utils
|
||||
|
||||
import "os"
|
||||
|
||||
func HasPermission(filePath string, perm os.FileMode) (bool, error) {
|
||||
fileInfo, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
mode := fileInfo.Mode().Perm()
|
||||
if mode&perm == perm {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func AddPermission(filePath string, perm os.FileMode) error {
|
||||
fileInfo, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mode := fileInfo.Mode() | perm
|
||||
err = os.Chmod(filePath, mode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func EnsurePermission(filePath string, perm os.FileMode) error {
|
||||
hasPerm, err := HasPermission(filePath, perm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !hasPerm {
|
||||
err = AddPermission(filePath, perm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user