From d279ad552a2ba6d742ef43a36f6176ffbcc27465 Mon Sep 17 00:00:00 2001 From: mzz2017 Date: Sat, 26 Dec 2020 00:31:17 +0800 Subject: [PATCH] feat: outline upstraem support link of shadowbox_config file. #2 --- config/outline.go | 44 +++++++++++++++++++++++++++++++++---------- example_fullview.json | 5 +++++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/config/outline.go b/config/outline.go index cde9285..5639b10 100644 --- a/config/outline.go +++ b/config/outline.go @@ -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 != "" { @@ -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(), @@ -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) } @@ -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 { @@ -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 diff --git a/example_fullview.json b/example_fullview.json index aab16e9..ac089f3 100644 --- a/example_fullview.json +++ b/example_fullview.json @@ -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\ndt1oQDcv20won8ooUmqVaTWc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jg6Df+LlCGBEu9mfHddB52kwQFxj55XBrWhnQby/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": [