Skip to content

Commit

Permalink
feat: outline upstraem support link of shadowbox_config file. #2
Browse files Browse the repository at this point in the history
  • Loading branch information
mzz2017 committed Dec 25, 2020
1 parent 3502e03 commit d279ad5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
44 changes: 34 additions & 10 deletions config/outline.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,39 @@ import (
"encoding/json"
"fmt"
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
"net"
"net/http"
"strconv"
"time"
)

type Outline struct {
Type string `json:"type"`
Server string `json:"server"`
Link string `json:"link"`
SSHPort string `json:"sshPort"`
SSHUsername string `json:"sshUsername"`
SSHPrivateKey string `json:"sshPrivateKey"`
SSHPassword string `json:"sshPassword"`
}

func (outline Outline) getConfig() ([]byte, error) {
func (outline Outline) getConfigFromLink() ([]byte, error) {
client := http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Get(outline.Link)
if err != nil {
return nil, fmt.Errorf("getConfigFromLink failed: %v", err)
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}

func (outline Outline) getConfigFromSSH() ([]byte, error) {
var (
config *ssh.ClientConfig
conf *ssh.ClientConfig
authMethods []ssh.AuthMethod
)
if outline.SSHPrivateKey != "" {
Expand All @@ -35,7 +52,7 @@ func (outline Outline) getConfig() ([]byte, error) {
if username == "" {
username = "root"
}
config = &ssh.ClientConfig{
conf = &ssh.ClientConfig{
User: username,
Auth: authMethods,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Expand All @@ -44,7 +61,7 @@ func (outline Outline) getConfig() ([]byte, error) {
if port == "" {
port = "22"
}
client, err := ssh.Dial("tcp", net.JoinHostPort(outline.Server, port), config)
client, err := ssh.Dial("tcp", net.JoinHostPort(outline.Server, port), conf)
if err != nil {
return nil, fmt.Errorf("failed to dial: %v", err)
}
Expand All @@ -69,16 +86,23 @@ func (outline Outline) GetServers() (servers []Server, err error) {
err = fmt.Errorf("outline.GetGroups: %v", err)
}
}()
b, err := outline.getConfig()
var b []byte
if outline.Link != "" {
b, err = outline.getConfigFromLink()
}
if err != nil {
log.Printf("[warning] %v\n", err)
b, err = outline.getConfigFromSSH()
}
if err != nil {
return
}
var config ShadowboxConfig
err = json.Unmarshal(b, &config)
var conf ShadowboxConfig
err = json.Unmarshal(b, &conf)
if err != nil {
return
}
return config.ToServers(outline.Server), nil
return conf.ToServers(outline.Server), nil
}

type AccessKey struct {
Expand All @@ -103,9 +127,9 @@ type ShadowboxConfig struct {
NextID int `json:"nextId"`
}

func (config *ShadowboxConfig) ToServers(host string) []Server {
func (c *ShadowboxConfig) ToServers(host string) []Server {
var servers []Server
for _, k := range config.AccessKeys {
for _, k := range c.AccessKeys {
servers = append(servers, k.ToServer(host))
}
return servers
Expand Down
5 changes: 5 additions & 0 deletions example_fullview.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"sshPort": "22",
"sshUsername": "outline",
"sshPrivateKey": "-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn\nNhAAAAAwEAAQAAAYEAynXZl9Uu+XmZ+tFNWxG6hn0FrIC+BgNvtsiqdLag7P/xpJ+84sSS\nT032JydbYCbkD2E+qLAJXdH6ZFw65HKj31kYo8jWUUjZLe5WZFEVunI027/s496SI6e6o9\n2pRjyAFmh3s1sd/a1HOWLYCeyOHEt2SVB/EKxlf/KMC6z4JDmZmMtUquPYXBCDPbwhG6jW\nIyYqNv7d/G6cno6CK0WuyHueufSEn8AFRRv4ptunR1xWv4JSavqnHubJJxBFMmSE8pNren\nZy94ccLpOTIif+lbq2F3DmzMCM+8xbrv8EfQmwLz+Kb3be8zORcHGgiv2CJ4hoa/joBA7U\n25rQ+KFE4m9T5ro7Gf2vbjuPNDTgHe4twUM0dM3ue5p2eL3VEFpqcDdhz27SVWZXPzXrui\ndt1oQDcv20won8ooUmqVaTWc2E+Sj4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CGBEu9mfHddB52kwQFxj55XBrWhnQby/e8Yn0gERLWTk\nD8iNPtiUSs4tWPIUlHk1THHY5kGBurDA84XGbYtLfuHAQ8p6jshNcpPScGATAKaw2BABAA\nwQCA8zyHFFGYrpm3KFMho6jnkqAcDVijtiStubX25YHaeQi5e83y1TZvh31zvj3LhApz6d\ngUw546O/5ucMO3B+xOWHITxfHoen5UxjkRZML9Ob8wfZEppB9hAtTJHe985qG1rwfxAXXO\nZYDSyowGBJQ+e/FgaSIm5bQkCxHupTXTLk3rPxEXvZI1+BnYzu7rqj6tcUChJ9tgagJBAy\nJkMkEJha4U6NQi0jXvYPTzQXPASrpg3tFjXMAC1mb5V02s2vwAAADBAPFl5K4CH5aS6/Op\nhuGvaLVMMHthDUCMk5nmrUIX5IOVQSB5Y74bPqst0U7qs56Y40XoAxgV8oti7RpjQiueOx\n2co5usU1oWVVHQSyUT2YTOSMnwOe5CMeigAXlgVokmoxEOB9lRDD6L2i1KpIxJkWMPvtS6\nKTIyWprA9JTyZ2lSo5Raj5ncNcQSpdxPoW25HEgqUi7XA8ftjx7bMYMoHT2wzZHoQvnDpk\nETe3PAyNjwwTVypMuIrTh4YZbLrUDk2wAAAMEA1rT/WjvdgMiup3eFOfqR0Og4YcUBmjwP\nUXcEY65kTDA0U8WoHizq/vUQ2tQhHjM2vASFvO9M079l4+jDj3dzd2qeSw4V5AYJmnhMqZ\n58LXgKQWZswEWnkKKIs5S+OQkcZw1iGTodipFcWW6nUUsaw1TY1esFLMcpeMeAYZ/sgADp\n7XTO1A7WWEG2dQWVVA/SfaFuimgnBOU7EKmbiGjPQFfxGCmgg5pBjNd528ExRw6pGX9b3d\n0hiMiJp6XWOcqJAAAAC216ekBtenprZXRpAQIDBAUGBw==\n-----END OPENSSH PRIVATE KEY-----\n"
},
{
"type": "outline",
"server": "131.13.130.120",
"link": "https://example.com/3BlbnNzaC1rZXktdjEAAAAABG/shadowbox_config.json"
}
],
"servers": [
Expand Down

0 comments on commit d279ad5

Please sign in to comment.