Skip to content

Commit

Permalink
Create VPN model and implemented store version logic for IOS
Browse files Browse the repository at this point in the history
  • Loading branch information
jigar-f committed Sep 1, 2023
1 parent 06897bd commit b2c1695
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 6 deletions.
1 change: 0 additions & 1 deletion internalsdk/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func (m *baseModel) Subscribe(req *SubscriptionRequest) error {
val := minisql.NewValue(convertedValue)
// Need wrap to coz we need to send to json
myVal := &MyValue{Value: *val}
log.Debugf("my val type %v", val.Type)
updatesMap[k] = &ItemInterface{
Path: itemWithRaw.Path,
DetailPath: itemWithRaw.DetailPath,
Expand Down
32 changes: 27 additions & 5 deletions internalsdk/session_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const ACCEPTED_TERMS_VERSION = "accepted_terms_version"
const ADS_ENABLED = "adsEnabled"
const CAS_ADS_ENABLED = "casAsEnabled"
const CURRENT_TERMS_VERSION = 1
const IS_PLAY_VERSION = "playVersion"

// NewSessionModel initializes a new SessionModel instance.
func NewSessionModel(schema string, mdb minisql.DB) (*SessionModel, error) {
Expand Down Expand Up @@ -148,6 +149,15 @@ func (s *SessionModel) InvokeMethod(method string, arguments minisql.Values) (*m
} else {
return minisql.NewValueBool(true), nil
}

case "setStoreVersion":
IsStoreVersion := arguments.Get(0)
err := setStoreVersion(s.baseModel, IsStoreVersion.Bool())
if err != nil {
return nil, err
} else {
return minisql.NewValueBool(true), nil
}
default:

return s.baseModel.InvokeMethod(method, arguments)
Expand Down Expand Up @@ -349,9 +359,12 @@ func setProvider(m *baseModel, provider string) error {
return nil
}

// Todo: Change this method name to IsStoreVersion
func (s *SessionModel) IsPlayVersion() (bool, error) {
// For now return static to yes
func (s *SessionModel) IsStoreVersion() (bool, error) {
osStoreVersion, err := s.db.Get(IS_PLAY_VERSION)
panicIfNecessary(err)
if string(osStoreVersion) == "true" {
return true, nil
}
return false, nil
}

Expand Down Expand Up @@ -399,15 +412,16 @@ func setProUser(m *baseModel, isPro bool) error {

func (s *SessionModel) SetReplicaAddr(replicaAddr string) error {
pathdb.Mutate(s.db, func(tx pathdb.TX) error {
pathdb.Put[string](tx, REPLICA_ADDR, replicaAddr, "")
//For now force replicate to disbale it
pathdb.Put[string](tx, REPLICA_ADDR, "", "")
return nil
})
return nil
}

func (s *SessionModel) ForceReplica() bool {
// return static for now
return true
return false
}

func (s *SessionModel) SetChatEnabled(chatEnable bool) {
Expand Down Expand Up @@ -449,3 +463,11 @@ func acceptTerms(m *baseModel) error {
})
return nil
}

func setStoreVersion(m *baseModel, isStoreVersion bool) error {
pathdb.Mutate(m.db, func(tx pathdb.TX) error {
pathdb.Put[bool](tx, IS_PLAY_VERSION, isStoreVersion, "")
return nil
})
return nil
}
51 changes: 51 additions & 0 deletions internalsdk/vpn_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package internalsdk

import (
"github.com/getlantern/pathdb"
"github.com/getlantern/pathdb/minisql"
)

// Custom Model implemnation
// VPNModel is a custom model derived from the baseModel.
type VpnModel struct {
*baseModel
}

const PATH_VPN_STATUS = "/vpn_status"
const PATH_SERVER_INFO = "/server_info"
const PATH_BANDWIDTH = "/bandwidth"

// NewSessionModel initializes a new SessionModel instance.
func NewVpnModel(schema string, mdb minisql.DB) (*VpnModel, error) {
base, err := newModel(schema, mdb)
if err != nil {
return nil, err
}
initVpnModel(base.(*baseModel))
model := &VpnModel{base.(*baseModel)}
return model, nil
}

func initVpnModel(m *baseModel) error {
pathdb.Mutate(m.db, func(tx pathdb.TX) error {
rawStatus, err := tx.Get(PATH_VPN_STATUS)
panicIfNecessary(err)
status := string(rawStatus)
if status != "" {
pathdb.Put[string](tx, PATH_VPN_STATUS, status, "")
} else {
pathdb.Put[string](tx, PATH_VPN_STATUS, "disconnected", "")
}
return nil
})
return nil
}

func (s *VpnModel) InvokeMethod(method string, arguments minisql.Values) (*minisql.Value, error) {
switch method {
case "Hello":
return minisql.NewValueString("Hello"), nil
default:
return s.baseModel.InvokeMethod(method, arguments)
}
}
8 changes: 8 additions & 0 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
03F2FE342A6949EF0082B34C /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F2FE332A6949EF0082B34C /* Logger.swift */; };
03F4CD7E2A77DE8500F7BDD8 /* SessionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F4CD7D2A77DE8500F7BDD8 /* SessionManager.swift */; };
03F4CD822A77E43C00F7BDD8 /* BaseModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F4CD812A77E43C00F7BDD8 /* BaseModel.swift */; };
03FAF1B22AA1C7940063580C /* RunningEnv.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03FAF1B12AA1C7940063580C /* RunningEnv.swift */; };
03FAF1B42AA1E9F40063580C /* VpnModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03FAF1B32AA1E9F40063580C /* VpnModel.swift */; };
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
Expand Down Expand Up @@ -52,6 +54,8 @@
03F2FE332A6949EF0082B34C /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
03F4CD7D2A77DE8500F7BDD8 /* SessionManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = SessionManager.swift; path = Models/SessionManager.swift; sourceTree = "<group>"; };
03F4CD812A77E43C00F7BDD8 /* BaseModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = BaseModel.swift; path = Models/BaseModel.swift; sourceTree = "<group>"; };
03FAF1B12AA1C7940063580C /* RunningEnv.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RunningEnv.swift; sourceTree = "<group>"; };
03FAF1B32AA1E9F40063580C /* VpnModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = VpnModel.swift; path = Models/VpnModel.swift; sourceTree = "<group>"; };
04AF578F28344E5CF0932AA9 /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -119,6 +123,7 @@
03F4CD7D2A77DE8500F7BDD8 /* SessionManager.swift */,
03F4CD812A77E43C00F7BDD8 /* BaseModel.swift */,
0369DF782A93803D000EBE43 /* MessagingModel.swift */,
03FAF1B32AA1E9F40063580C /* VpnModel.swift */,
);
name = Models;
sourceTree = "<group>";
Expand All @@ -129,6 +134,7 @@
03F2FE332A6949EF0082B34C /* Logger.swift */,
03026EA22A77D67A001D5507 /* ValueUtil.swift */,
036663D82A9E0C0E00595971 /* JsonUtils.swift */,
03FAF1B12AA1C7940063580C /* RunningEnv.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -366,9 +372,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
03FAF1B22AA1C7940063580C /* RunningEnv.swift in Sources */,
03F2FE342A6949EF0082B34C /* Logger.swift in Sources */,
03F4CD7E2A77DE8500F7BDD8 /* SessionManager.swift in Sources */,
0369DF792A93803D000EBE43 /* MessagingModel.swift in Sources */,
03FAF1B42AA1E9F40063580C /* VpnModel.swift in Sources */,
03F4CD822A77E43C00F7BDD8 /* BaseModel.swift in Sources */,
035BE7062A7122BC0084059A /* SessionModel.swift in Sources */,
036663D92A9E0C0E00595971 /* JsonUtils.swift in Sources */,
Expand Down
3 changes: 3 additions & 0 deletions ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Internalsdk
var sessionModel:SessionModel!
var messagingModel:MessagingModel!
var lanternModel:LanternModel!
var vpnModel:VpnModel!
var flutterbinaryMessenger:FlutterBinaryMessenger!
var lanternMethodChannel:FlutterMethodChannel!
var navigationChannel:FlutterMethodChannel!
Expand All @@ -36,6 +37,8 @@ import Internalsdk
messagingModel=MessagingModel(flutterBinary: flutterbinaryMessenger)
//Init Lantern Model
lanternModel=LanternModel(flutterBinary: flutterbinaryMessenger)
//Init VPN Model
vpnModel=VpnModel(flutterBinary: flutterbinaryMessenger)
}


Expand Down
13 changes: 13 additions & 0 deletions ios/Runner/Lantern/Models/BaseModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Flutter
enum ModelType {
case sessionModel
case messagingModel
case vpnModel
}


Expand Down Expand Up @@ -56,6 +57,11 @@ open class BaseModel<T>: NSObject ,FlutterStreamHandler{
throw error!
}
self.model = createdModel as! T
case .vpnModel:
guard let createdModel = InternalsdkNewVpnModel(self.schema, swiftDB, &error) else {
throw error!
}
self.model = createdModel as! T
}

} catch {
Expand Down Expand Up @@ -84,7 +90,10 @@ open class BaseModel<T>: NSObject ,FlutterStreamHandler{
modelName = "session"
case .messagingModel:
modelName = "messaging"
case .vpnModel:
modelName = "vpn"
}

eventChannel = FlutterEventChannel(name: "\(modelName)_event_channel", binaryMessenger: binaryMessenger)
eventChannel.setStreamHandler(self)

Expand Down Expand Up @@ -182,6 +191,8 @@ open class BaseModel<T>: NSObject ,FlutterStreamHandler{
try sessionModelSub.subscribe(subscriber)
case let messagingModel as InternalsdkMessagingModel:
try messagingModel.subscribe(subscriber)
case let vpnModel as InternalsdkVpnModel:
try vpnModel.subscribe(subscriber)
default:
throw NSError(domain: "UnsupportedModel", code: 999, userInfo: ["Description": "Unsupported model type."])
}
Expand All @@ -193,6 +204,8 @@ open class BaseModel<T>: NSObject ,FlutterStreamHandler{
try sessionSub.unsubscribe(subscriberID)
case let messagingModel as InternalsdkMessagingModel:
try messagingModel.unsubscribe(subscriberID)
case let vpnModel as InternalsdkVpnModel:
try vpnModel.unsubscribe(subscriberID)
default:
throw NSError(domain: "UnsupportedModel", code: 999, userInfo: ["Description": "Unsupported model type."])
}
Expand Down
15 changes: 15 additions & 0 deletions ios/Runner/Lantern/Models/SessionModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SessionModel:BaseModel<InternalsdkSessionModel> {
setTimeZone()
setReferalCode()
initializeSessionModel()
storeVersion()
}


Expand Down Expand Up @@ -104,6 +105,20 @@ class SessionModel:BaseModel<InternalsdkSessionModel> {
}
}


func storeVersion(){
let miniSqlValue = ValueUtil.convertToMinisqlValue(isRunningFromAppStore())
if(miniSqlValue != nil){
do {
let result = try invokeMethodOnGo(name: "setStoreVersion", argument: miniSqlValue!)
logger.log("This is app store version \(result)")
}catch{
logger.log("Error while setting storeVersion")
}
}
}



func setForceCountry(countryCode:String){
let countryMiniSql = ValueUtil.convertToMinisqlValue(countryCode)
Expand Down
28 changes: 28 additions & 0 deletions ios/Runner/Lantern/Models/VpnModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// VpnModel.swift
// Runner
//
// Created by jigar fumakiya on 01/09/23.
//


import Foundation
import Internalsdk
import Flutter


class VpnModel : BaseModel<InternalsdkVpnModel> {

var flutterbinaryMessenger:FlutterBinaryMessenger

init(flutterBinary:FlutterBinaryMessenger) {
self.flutterbinaryMessenger=flutterBinary
super.init(type: .vpnModel , flutterBinary: self.flutterbinaryMessenger)
initializeVpnModel()
}

func initializeVpnModel() {

}

}
64 changes: 64 additions & 0 deletions ios/Runner/Lantern/Utils/RunningEnv.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// RunningEnv.swift
// Runner
//
// Created by jigar fumakiya on 01/09/23.

import Foundation


func isRunningFromAppStore() -> Bool {
let file = "\(NSHomeDirectory())/iTunesMetadata.plist"
if FileManager.default.fileExists(atPath: file) {
// The app is running from the App Store
return true
} else {
// The app is not running from the App Store
return false
}
}


func isRunningInTestFlightEnvironment() -> Bool{
if isSimulator() {
return false
} else {
if isAppStoreReceiptSandbox() && !hasEmbeddedMobileProvision() {
return true
} else {
return false
}
}
}


private func hasEmbeddedMobileProvision() -> Bool{
if let _ = Bundle.main.path(forResource: "embedded", ofType: "mobileprovision") {
return true
}
return false
}


func isAppStoreReceiptSandbox() -> Bool {
if isSimulator() {
return false
} else {
if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL {
if appStoreReceiptURL.lastPathComponent == "sandboxReceipt" {
return true
}
}
return false
}
}



private func isSimulator() -> Bool {
#if arch(i386) || arch(x86_64)
return true
#else
return false
#endif
}

0 comments on commit b2c1695

Please sign in to comment.