-
-
Notifications
You must be signed in to change notification settings - Fork 375
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
Implement support for Wireguard in PIA #1836
base: master
Are you sure you want to change the base?
Changes from all commits
9d41b3e
61c76dc
fdca594
4dc40fe
ea7c224
fa4b3c1
a95d3fb
1fdda76
e22fd60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
scratch.txt | ||
.idea/ | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ var ( | |
ErrWireguardPublicKeyEmpty = errors.New("wireguard public key field is empty") | ||
) | ||
|
||
func (s *Server) HasMinimumInformation() (err error) { | ||
func (s *Server) HasMinimumInformation(providerWireguardKeyUnknown bool) (err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead check the |
||
switch { | ||
case s.VPN == "": | ||
return fmt.Errorf("%w", ErrVPNFieldEmpty) | ||
|
@@ -54,7 +54,7 @@ func (s *Server) HasMinimumInformation() (err error) { | |
return fmt.Errorf("%w", ErrNetworkProtocolSet) | ||
case s.VPN == vpn.OpenVPN && !s.TCP && !s.UDP: | ||
return fmt.Errorf("%w", ErrNoNetworkProtocol) | ||
case s.VPN == vpn.Wireguard && s.WgPubKey == "": | ||
case s.VPN == vpn.Wireguard && (s.WgPubKey == "" && !providerWireguardKeyUnknown): | ||
return fmt.Errorf("%w", ErrWireguardPublicKeyEmpty) | ||
default: | ||
return nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,7 +100,7 @@ func Test_replaceInString(t *testing.T) { | |
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
result := replaceInString(testCase.s, testCase.substitutions) | ||
result := ReplaceInString(testCase.s, testCase.substitutions) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please keep this unexported (lowercase first letter), there is no need to export it |
||
assert.Equal(t, testCase.result, result) | ||
}) | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,72 @@ | ||||||
package privateinternetaccess | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename file to |
||||||
|
||||||
import ( | ||||||
"context" | ||||||
"encoding/json" | ||||||
"fmt" | ||||||
"net/http" | ||||||
"net/url" | ||||||
"strings" | ||||||
) | ||||||
|
||||||
func fetchToken(ctx context.Context, client *http.Client, | ||||||
tokenType string, authFilePath string) (token string, err error) { | ||||||
Comment on lines
+12
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since this is used outside openvpn-only, we should inject the username and password and not use the openvpn auth file anymore (especially if it's for Wireguard, that makes it super strange) |
||||||
username, password, err := getOpenvpnCredentials(authFilePath) | ||||||
if err != nil { | ||||||
return "", fmt.Errorf("getting username and password: %w", err) | ||||||
} | ||||||
|
||||||
errSubstitutions := map[string]string{ | ||||||
url.QueryEscape(username): "<username>", | ||||||
url.QueryEscape(password): "<password>", | ||||||
} | ||||||
|
||||||
var path string | ||||||
|
||||||
switch tokenType { | ||||||
case "client": | ||||||
path = "/api/client/v2/token" | ||||||
case "gtoken": | ||||||
path = "/gtoken/generateToken" | ||||||
default: | ||||||
return "", fmt.Errorf("token type %q is not supported", tokenType) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could even panic here, since that would be a programming error:
Suggested change
|
||||||
} | ||||||
|
||||||
form := url.Values{} | ||||||
form.Add("username", username) | ||||||
form.Add("password", password) | ||||||
url := url.URL{ | ||||||
Scheme: "https", | ||||||
Host: "www.privateinternetaccess.com", | ||||||
Path: path, | ||||||
} | ||||||
request, err := http.NewRequestWithContext(ctx, http.MethodPost, url.String(), strings.NewReader(form.Encode())) | ||||||
if err != nil { | ||||||
return "", ReplaceInErr(err, errSubstitutions) | ||||||
} | ||||||
|
||||||
request.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||||||
|
||||||
response, err := client.Do(request) | ||||||
if err != nil { | ||||||
return "", ReplaceInErr(err, errSubstitutions) | ||||||
} | ||||||
defer response.Body.Close() | ||||||
|
||||||
if response.StatusCode != http.StatusOK { | ||||||
return "", makeNOKStatusError(response, errSubstitutions) | ||||||
} | ||||||
|
||||||
decoder := json.NewDecoder(response.Body) | ||||||
var result struct { | ||||||
Token string `json:"token"` | ||||||
} | ||||||
if err := decoder.Decode(&result); err != nil { | ||||||
return "", fmt.Errorf("decoding response: %w", err) | ||||||
} | ||||||
|
||||||
if result.Token == "" { | ||||||
return "", errEmptyToken | ||||||
} | ||||||
return result.Token, nil | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove before merging