Skip to content

Commit

Permalink
enable authorize device for pro screen, add request link code to pro …
Browse files Browse the repository at this point in the history
…client
  • Loading branch information
atavism committed Dec 5, 2023
1 parent d7620b3 commit 8e4b162
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 29 deletions.
9 changes: 9 additions & 0 deletions desktop/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ func ProUser() *C.char {
return C.CString("false")
}

//export DeviceLinkingCode
func DeviceLinkingCode() *C.char {
resp, err := proClient.LinkCodeRequest()
if err != nil {
return sendError(err)
}
return C.CString(resp.Code)
}

//export DevelopmentMode
func DevelopmentMode() *C.char {
return C.CString("false")
Expand Down
45 changes: 45 additions & 0 deletions desktop/pro/pro.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package pro

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"strconv"

"github.com/shirou/gopsutil/v3/host"

"github.com/getlantern/golog"
"github.com/getlantern/flashlight/v7/pro"
"github.com/getlantern/lantern-client/desktop/app"
)

const (
baseUrl = "https://api.getiantem.org"
linkCodeUrl = baseUrl + "/link-code-request"
paymentMethodsUrl = baseUrl + "/plans-v3"
plansUrl = baseUrl + "/plans"
userDetailsUrl = baseUrl + "/user-data"
Expand Down Expand Up @@ -60,6 +66,45 @@ func (pc *ProClient) newRequest(method, url string) (*http.Request, error) {
return req, nil
}

func (pc *ProClient) POST(url string, body io.Reader) (*http.Response, error) {
// Create a new request
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
log.Errorf("Error creating user details request: %v", err)
return nil, err
}
req.Header.Set("Content-Type", "application/json")

pc.setHeaders(req)

// Send the request
resp, err := pc.Do(req)
if err != nil {
log.Errorf("Error sending user details request: %v", err)
return nil, err
}
return resp, nil
}

func (pc *ProClient) LinkCodeRequest() (*LinkCodeResponse, error) {
info, _ := host.Info()
jsonBody := []byte(fmt.Sprintf(`{"deviceName": "%s"}`, info.Hostname))
resp, err := pc.POST(linkCodeUrl, bytes.NewReader(jsonBody))
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Read the response body
var linkCodeResponse LinkCodeResponse
// Read and decode the response body
if err := json.NewDecoder(resp.Body).Decode(&linkCodeResponse); err != nil {
log.Errorf("Error decoding response body: %v", err)
return nil, err
}
return &linkCodeResponse, nil

}

func (pc *ProClient) Plans() (*PlansResponse, error) {
// Create a new request
req, err := pc.newRequest("GET", plansUrl)
Expand Down
106 changes: 89 additions & 17 deletions desktop/pro/pro.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion desktop/pro/pro.proto
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ message PaymentMethod {

message PaymentMethodsResponse {
map<string, google.protobuf.ListValue> providers = 1;
}
}

message LinkCodeResponse {
string code = 1;
int64 expireAt = 2;
}
15 changes: 7 additions & 8 deletions lib/account/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ class AccountMenu extends StatelessWidget {
inviteFriends(context);
},
),
if (Platform.isAndroid)
ListItemFactory.settingsItem(
icon: ImagePaths.devices,
content: 'Authorize Device for Pro'.i18n,
onTap: () {
authorizeDeviceForPro(context);
},
),
ListItemFactory.settingsItem(
icon: ImagePaths.devices,
content: 'Authorize Device for Pro'.i18n,
onTap: () {
authorizeDeviceForPro(context);
},
),
...commonItems(context)
];
}
Expand Down
2 changes: 1 addition & 1 deletion lib/account/device_linking/link_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class _LinkDeviceState extends State<LinkDevice> {
@override
void initState() {
super.initState();
sessionModel.requestLinkCode();
if (Platform.isAndroid) sessionModel.requestLinkCode();
}

@override
Expand Down
12 changes: 10 additions & 2 deletions lib/common/session_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,17 @@ class SessionModel extends Model {
}

Widget deviceLinkingCode(ValueWidgetBuilder<String> builder) {
return subscribedSingleValueBuilder<String>(
'devicelinkingcode',
if (Platform.isAndroid) {
return subscribedSingleValueBuilder<String>(
'devicelinkingcode',
defaultValue: '',
builder: builder,
);
}
return ffiValueBuilder<String>(
'deviceLinkingCode',
defaultValue: '',
ffiDeviceLinkingCode,
builder: builder,
);
}
Expand Down
1 change: 1 addition & 0 deletions lib/ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ final ProFunc ffiCountry = dylib.lookup<ffi.NativeFunction<pro_func>>('Country')
final ProFunc ffiLang = dylib.lookup<ffi.NativeFunction<pro_func>>('Lang').asFunction();
final ProFunc ffiAcceptedTermsVersion = dylib.lookup<ffi.NativeFunction<pro_func>>('AcceptedTermsVersion').asFunction();
final ProFunc ffiProUser = dylib.lookup<ffi.NativeFunction<pro_func>>('ProUser').asFunction();
final ProFunc ffiDeviceLinkingCode = dylib.lookup<ffi.NativeFunction<pro_func>>('DeviceLinkingCode').asFunction();
final ProFunc ffiDevelopmentMode = dylib.lookup<ffi.NativeFunction<pro_func>>('DevelopmentMode').asFunction();
final ProFunc ffiSplitTunneling = dylib.lookup<ffi.NativeFunction<pro_func>>('SplitTunneling').asFunction();
final ProFunc ffiChatMe = dylib.lookup<ffi.NativeFunction<pro_func>>('ChatMe').asFunction();
Expand Down

0 comments on commit 8e4b162

Please sign in to comment.