forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go style commands, merge v2ctl commands, v5 oriented (v2fly#369)
* go style commands merge v2ctl commandsw * migrate go style commands to v2ctl * fixes & code optimize * sort the commands * update commands description * restore old proto golang.org proto has removed UnmarshalText, without alternative * add test command * remove unused code * code optimize and fix * The commit simplifies the run and test commands code, * Fixes a hidden issue that the format flag not applied in command "v2ray test -format=pb ..." * fix default loader logic
- Loading branch information
Showing
33 changed files
with
1,207 additions
and
854 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package all | ||
|
||
import "v2ray.com/core/commands/base" | ||
|
||
// go:generate go run v2ray.com/core/common/errors/errorgen | ||
|
||
func init() { | ||
base.RootCommand.Commands = append( | ||
base.RootCommand.Commands, | ||
cmdAPI, | ||
cmdConvert, | ||
cmdLove, | ||
cmdTLS, | ||
cmdUUID, | ||
cmdVerify, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package all | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"google.golang.org/protobuf/proto" | ||
"v2ray.com/core/commands/base" | ||
"v2ray.com/core/common" | ||
"v2ray.com/core/common/buf" | ||
"v2ray.com/core/infra/conf" | ||
"v2ray.com/core/infra/conf/serial" | ||
) | ||
|
||
var cmdConvert = &base.Command{ | ||
UsageLine: "{{.Exec}} convert [json file] [json file] ...", | ||
Short: "Convert multiple json config to protobuf", | ||
Long: ` | ||
Convert multiple json config to protobuf. | ||
Examples: | ||
{{.Exec}} {{.LongName}} config.json c1.json c2.json <url>.json | ||
`, | ||
} | ||
|
||
func init() { | ||
cmdConvert.Run = executeConvert // break init loop | ||
} | ||
|
||
func executeConvert(cmd *base.Command, args []string) { | ||
unnamedArgs := cmdConvert.Flag.Args() | ||
if len(unnamedArgs) < 1 { | ||
base.Fatalf("empty config list") | ||
} | ||
|
||
conf := &conf.Config{} | ||
for _, arg := range unnamedArgs { | ||
fmt.Fprintf(os.Stderr, "Read config: %s", arg) | ||
r, err := loadArg(arg) | ||
common.Must(err) | ||
c, err := serial.DecodeJSONConfig(r) | ||
if err != nil { | ||
base.Fatalf(err.Error()) | ||
} | ||
conf.Override(c, arg) | ||
} | ||
|
||
pbConfig, err := conf.Build() | ||
if err != nil { | ||
base.Fatalf(err.Error()) | ||
} | ||
|
||
bytesConfig, err := proto.Marshal(pbConfig) | ||
if err != nil { | ||
base.Fatalf("failed to marshal proto config: %s", err) | ||
} | ||
|
||
if _, err := os.Stdout.Write(bytesConfig); err != nil { | ||
base.Fatalf("failed to write proto config: %s", err) | ||
} | ||
} | ||
|
||
// loadArg loads one arg, maybe an remote url, or local file path | ||
func loadArg(arg string) (out io.Reader, err error) { | ||
var data []byte | ||
switch { | ||
case strings.HasPrefix(arg, "http://"), strings.HasPrefix(arg, "https://"): | ||
data, err = FetchHTTPContent(arg) | ||
|
||
case arg == "stdin:": | ||
data, err = ioutil.ReadAll(os.Stdin) | ||
|
||
default: | ||
data, err = ioutil.ReadFile(arg) | ||
} | ||
|
||
if err != nil { | ||
return | ||
} | ||
out = bytes.NewBuffer(data) | ||
return | ||
} | ||
|
||
// FetchHTTPContent dials https for remote content | ||
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 | ||
} |
2 changes: 1 addition & 1 deletion
2
infra/control/errors.generated.go → commands/all/errors.generated.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package control | ||
package all | ||
|
||
import "v2ray.com/core/common/errors" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package all | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"compress/gzip" | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"v2ray.com/core/commands/base" | ||
"v2ray.com/core/common" | ||
"v2ray.com/core/common/platform" | ||
) | ||
|
||
var cmdLove = &base.Command{ | ||
UsageLine: "{{.Exec}} lovevictoria", | ||
Short: "", // set Short to "" hides the command | ||
Long: "", | ||
Run: executeLove, | ||
} | ||
|
||
func executeLove(cmd *base.Command, args []string) { | ||
const content = "H4sIAAAAAAAC/4SVMaskNwzH+/kUW6izcSthMGrcqLhVk0rdQS5cSMg7Xu4S0vizB8meZd57M3ta2GHX/ukvyZZmY2ZKDMzCzJyY5yOlxKII1omsf+qkBiiC6WhbYsbkjDAfySQsJqD3jtrD0EBM3sBHzG3kUsrglIQREXonpd47kYIi4AHmgI9Wcq2jlJITC6JZJ+v3ECYzBMAHyYm392yuY4zWsjACmHZSh6l3A0JETzGlWZqDsnArpTg62mhJONhOdO90p97V1BAnteoaOcuummtrrtuERQwUiJwP8a4KGKcyxdOCw1spOY+WHueFqmakAIgUSSuhwKNgobxKXSLbtg6r5cFmBiAeF6yCkYycmv+BiCIiW8ScHa3DgxAuZQbRhFNrLTFo96RBmx9jKWWG5nBsjyJzuIkftUblonppZU5t5LzwIks5L1a4lijagQxLokbIYwxfytNDC+XQqrWW9fzAunhqh5/Tg8PuaMw0d/Tcw3iDO81bHfWM/AnutMh2xqSUntMzd3wHDy9iHMQz8bmUZYvqedTJ5GgOnrNt7FIbSlwXE3wDI19n/KA38MsLaP4l89b5F8AV3ESOMIEhIBgezHBc0H6xV9KbaXwMvPcNvIHcC0C7UPZQx4JVTb35/AneSQq+bAYXsBmY7TCRupF2NTdVm/+ch22xa0pvRERKqt1oxj9DUbXzU84Gvj5hc5a81SlAUwMwgEs4T9+7sg9lb9h+908MWiKV8xtWciVTmnB3tivRjNerfXdxpfEBbq2NUvLMM5R9NLuyQg8nXT0PIh1xPd/wrcV49oJ6zbZaPlj2V87IY9T3F2XCOcW2MbZyZd49H+9m81E1N9SxlU+ff/1y+/f3719vf7788+Ugv/ffbMIH7ZNj0dsT4WMHHwLPu/Rp2O75uh99AK+N2xn7ZHq1OK6gczkN+9ngdOl1Qvki5xwSR8vFX6D+9vXA97B/+fr5rz9u/738uP328urP19vfP759e3n9Xs6jamvqlfJ/AAAA//+YAMZjDgkAAA==" | ||
c, err := base64.StdEncoding.DecodeString(content) | ||
common.Must(err) | ||
reader, err := gzip.NewReader(bytes.NewBuffer(c)) | ||
common.Must(err) | ||
b := make([]byte, 4096) | ||
nBytes, _ := reader.Read(b) | ||
|
||
bb := bytes.NewBuffer(b[:nBytes]) | ||
scanner := bufio.NewScanner(bb) | ||
for scanner.Scan() { | ||
s := scanner.Text() | ||
fmt.Print(s + platform.LineSeparator()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package all | ||
|
||
import ( | ||
"v2ray.com/core/commands/all/tlscmd" | ||
"v2ray.com/core/commands/base" | ||
) | ||
|
||
var cmdTLS = &base.Command{ | ||
UsageLine: "{{.Exec}} tls", | ||
Short: "TLS tools", | ||
Long: `{{.Exec}} tls provides tools for TLS. | ||
`, | ||
|
||
Commands: []*base.Command{ | ||
tlscmd.CmdCert, | ||
tlscmd.CmdPing, | ||
}, | ||
} |
Oops, something went wrong.