Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support https for both client and server #875

Merged
merged 9 commits into from
Dec 21, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor https upgrade
  • Loading branch information
samanhappy committed Dec 19, 2022
commit 91426a802705d2390a7fc441f07cc16381f59689
20 changes: 9 additions & 11 deletions cmd/client/command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ const (
)

func makeURL(urlTemplate string, a ...interface{}) string {
p := HTTPProtocal
if CommandlineGlobalFlags.ForceTLS {
p = HTTPSProtocal
}
return p + CommandlineGlobalFlags.Server + fmt.Sprintf(urlTemplate, a...)
return CommandlineGlobalFlags.Server + fmt.Sprintf(urlTemplate, a...)
}

func successfulStatusCode(code int) bool {
Expand All @@ -145,20 +141,22 @@ func handleRequest(httpMethod string, url string, yamlBody []byte, cmd *cobra.Co
}
}

p := HTTPProtocal
if CommandlineGlobalFlags.ForceTLS {
p = HTTPSProtocal
}
tr := http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: CommandlineGlobalFlags.InsecureSkipVerify},
}
client := &http.Client{Transport: &tr}
resp, body := doRequest(httpMethod, url, jsonBody, client, cmd)
resp, body := doRequest(httpMethod, p+url, jsonBody, client, cmd)

if resp.StatusCode == 400 && strings.Contains(string(body), "Client sent an HTTP request to an HTTPS server") {
fmt.Println("Warning: upgraded to HTTPS, it's better to turn on option --force-tls")
url = strings.ReplaceAll(url, HTTPProtocal, HTTPSProtocal)
resp, body = doRequest(httpMethod, url, jsonBody, client, cmd)
msg := string(body)
if resp.StatusCode == 400 && strings.Contains(msg, "HTTP") && strings.Contains(msg, "HTTPS") {
samanhappy marked this conversation as resolved.
Show resolved Hide resolved
resp, body = doRequest(httpMethod, HTTPSProtocal+url, jsonBody, client, cmd)
}

if !successfulStatusCode(resp.StatusCode) {
msg := string(body)
apiErr := &APIErr{}
err := codectool.Unmarshal(body, apiErr)
if err == nil {
Expand Down