Files
codefever/ssh-gateway/shell/main.go
Carney Wu d3de96487f initial version (#1)
* fix(Useless Code): remove useless code

* feat(Deploy Scripts): add deploy scripts

* fix(Delopy Script): change settings

* fix(Deploy Script): fix ssh-keygen script

* fix(Deploy Script): change env file path

* feat(Deploy Script): add db migration

* fix(Deploy script): change script

* feat(Deploy Script): add sql file to create database

* fix(Deploy Script): add composer support

* fix(Deploy Script): add composer

* fix(Service Script): add http gateway

* fix(Deploy Script): add git path

* fix(Deploy Script): fix setting bugs

* fix(Init Script): get user from config

* fix(Service): adjust run users

* feat(Doc): add doc

* fix(Doc): change docs

* fix(Deploy script): change owner of storage path

* feat: codefever-community documentation system

* fix(Doc): doc details page style

* feat: fix page navigation

* fix(SQL File): fix db file fit MySQL 5.7

* fix(FileTree): empty repository display

* fix: fix helper navigation

* docs(zh-cn essential part):

* fix(Doc Style): change markdown.css

* docs(contribution doc):

* fix: unified page style

* docs(Readme): add readme

* build(Build):

Co-authored-by: cubic <carneywu@pgyer.com>
Co-authored-by: pololi <pololi@pgyer.com>
Co-authored-by: yangchen <chenyang@pgyer.com>
2022-01-19 17:21:59 +08:00

136 lines
2.5 KiB
Go

package main
import (
"fmt"
"log"
"os"
"os/exec"
"regexp"
"io/ioutil"
"path/filepath"
"gopkg.in/yaml.v2"
"encoding/json"
"../JsonHttpClient"
)
type gateway struct {
Token string `yaml:"token"`
AuthServer string `yaml:"authSSH"`
}
type configType struct {
Gateway gateway `yaml:"gateway"`
}
type RequestData struct {
UserID string `json:"userID"`
Repo string `json:"repo"`
Action string `json:"action"`
}
type Payload struct {
Uid string `json:"uid"`
Path string `json:"path"`
Action string `json:"action"`
}
type ResponseData struct {
Code int `json:"code"`
Message string `json:"message"`
Data Payload `json:"data"`
}
func readConfig () configType {
var conf configType
// get current dir
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
fmt.Println(err.Error())
}
// get yaml file content
configData, err := ioutil.ReadFile(dir + "/../../env.yaml")
if err != nil {
fmt.Println(err.Error())
}
err = yaml.Unmarshal(configData, &conf)
if err != nil {
fmt.Println(err.Error())
}
return conf
}
var config configType
func init() {
file := "./" + "message" + ".txt"
logFile, err := os.OpenFile(file, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0766)
if err != nil {
panic(err)
}
log.SetOutput(logFile)
log.SetFlags(log.LstdFlags | log.Lshortfile | log.LUTC)
return
}
func main () {
// read yml config to global
config = readConfig()
if (len(os.Args) == 4) {
pattern, _ := regexp.Compile(`^(git-(receive|upload)-pack)$`)
sub := pattern.FindSubmatch([]byte(os.Args[1]))
if (len(sub) > 1) {
requestData, err := json.Marshal(RequestData{
UserID: os.Args[3],
Repo: os.Args[2],
Action: string(sub[1]),
})
if (err != nil) {
fmt.Println(err.Error())
return
}
status, response := jsonHttpClient.Request(config.Gateway.Token, "GET", config.Gateway.AuthServer, requestData)
responseData := &ResponseData{}
err = json.Unmarshal(response, responseData)
if (status != 200 || responseData.Code != 0) {
return
}
os.Setenv("PGYER_UID", responseData.Data.Uid)
cmd := exec.Command(os.Args[1], responseData.Data.Path)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if (err != nil) {
fmt.Println(err.Error())
return
}
err = cmd.Wait()
if (err != nil) {
fmt.Println(err.Error())
return
}
}
}
fmt.Println("[CodeFever Community]: Interaction shell is not allowed.")
return
}