1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-19 10:56:06 -05:00
v2fly/main/jsonem/jsonem.go

92 lines
1.9 KiB
Go
Raw Normal View History

package jsonem
import (
2019-12-14 09:24:32 -05:00
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"v2ray.com/core"
"v2ray.com/core/common"
2019-12-14 09:24:32 -05:00
"v2ray.com/core/common/buf"
"v2ray.com/core/infra/conf"
2019-02-10 13:04:11 -05:00
"v2ray.com/core/infra/conf/serial"
)
func init() {
common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
Name: "JSON",
Extension: []string{"json"},
2019-12-14 09:24:32 -05:00
Loader: func(input io.Reader) (*core.Config, error) {
fns := []string{}
data, _ := ioutil.ReadAll(input)
json.Unmarshal(data, &fns)
cf := &conf.Config{}
for _, arg := range fns {
r, err := LoadArg(arg)
common.Must(err)
c, err := serial.DecodeJSONConfig(r)
common.Must(err)
cf.Override(c, arg)
}
return cf.Build()
}}))
}
func LoadArg(arg string) (out io.Reader, err error) {
var data []byte
if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
data, err = FetchHTTPContent(arg)
} else {
data, err = ioutil.ReadFile(arg)
}
if err != nil {
return
}
out = bytes.NewBuffer(data)
return
}
func FetchHTTPContent(target string) ([]byte, error) {
parsedTarget, err := url.Parse(target)
if err != nil {
return nil, newError("invalid URL: ", target).Base(err)
}
if s := strings.ToLower(parsedTarget.Scheme); s != "http" && s != "https" {
return nil, newError("invalid scheme: ", parsedTarget.Scheme)
}
client := &http.Client{
Timeout: 30 * time.Second,
}
resp, err := client.Do(&http.Request{
Method: "GET",
URL: parsedTarget,
Close: true,
})
if err != nil {
return nil, newError("failed to dial to ", target).Base(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, newError("unexpected HTTP status code: ", resp.StatusCode)
}
content, err := buf.ReadAllToBytes(resp.Body)
if err != nil {
return nil, newError("failed to read HTTP response").Base(err)
}
return content, nil
}