Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
hossinasaadi committed Jul 23, 2023
1 parent 288f98c commit 53da43d
Show file tree
Hide file tree
Showing 47 changed files with 949 additions and 19 deletions.
13 changes: 13 additions & 0 deletions PacketTunnel/Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Header.h
// PacketTunnel
//
// Created by HossinAsaadi on 7/23/23.
//


#ifndef Header_h
#define Header_h
#import <libtun2socks/tun2socks.h>

#endif /* Header_h */
13 changes: 13 additions & 0 deletions PacketTunnel/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.networkextension.packet-tunnel</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).PacketTunnelProvider</string>
</dict>
</dict>
</plist>
20 changes: 20 additions & 0 deletions PacketTunnel/PacketTunnel.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.networking.networkextension</key>
<array>
<string>packet-tunnel-provider</string>
</array>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.hossin.asaadi.sshTunnel</string>
</array>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
</dict>
</plist>
96 changes: 96 additions & 0 deletions PacketTunnel/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// PacketTunnelProvider.swift
// PacketTunnel
//
// Created by HossinAsaadi on 7/23/23.
//

import NetworkExtension
import SshLib
class PacketTunnelProvider: NEPacketTunnelProvider {
var tunnelFd: Int32? {
var buf = [CChar](repeating: 0, count: Int(IFNAMSIZ))

for fd: Int32 in 0 ... 1024 {
var len = socklen_t(buf.count)

if getsockopt(fd, 2 /* IGMP */, 2, &buf, &len) == 0 && String(cString: buf).hasPrefix("utun") {
return fd
}
}

return packetFlow.value(forKey: "socket.fileDescriptor") as? Int32
}

override func startTunnel(options: [String : NSObject]?, completionHandler: @escaping (Error?) -> Void) {
let tunnelNetworkSettings = createTunnelSettings()
self.setTunnelNetworkSettings(tunnelNetworkSettings)

let localSocks = "127.0.0.1:10801"
let sshAddress = "1.1.1.1:22"
let sshUser = "root"
let sshPass = "1234"

let udpGwRemote = "1.1.1.1:7300"


DispatchQueue.global(qos: .userInitiated).async {

NSLog("run ssh : \(SshlibInitSSH(sshAddress,localSocks,sshUser,sshPass))")

}
DispatchQueue.global(qos: .userInitiated).async {

let argv = ["tun2socks",
"--netif-ipaddr",
"240.0.0.4",
"--netif-netmask",
"255.255.255.0",
"--loglevel",
"debug",
"--socks-server-addr",
localSocks,
"--udpgw-remote-server-addr",
udpGwRemote,
]
var cargs = argv.map { strdup($0) }

NSLog("run tun2socks_main: \(tun2socks_main(Int32(argv.count),&cargs,self.tunnelFd!,Int32(1500)))")


}

completionHandler(nil)

}

override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
completionHandler()
}

override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) {
// Add code here to handle the message.
if let handler = completionHandler {
handler(messageData)
}
}

override func sleep(completionHandler: @escaping () -> Void) {
// Add code here to get ready to sleep.
completionHandler()
}

override func wake() {
// Add code here to wake up.
}
func createTunnelSettings() -> NEPacketTunnelNetworkSettings {
let newSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "240.0.0.10")
newSettings.ipv4Settings = NEIPv4Settings(addresses: ["240.0.0.2"], subnetMasks: ["255.255.255.0"])
newSettings.ipv4Settings?.includedRoutes = [NEIPv4Route.`default`()]
newSettings.proxySettings = nil
newSettings.dnsSettings = NEDNSSettings(servers: ["8.8.8.8"])
newSettings.mtu = (1500) as NSNumber

return newSettings
}
}
53 changes: 53 additions & 0 deletions SshLib.xcframework/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AvailableLibraries</key>
<array>
<dict>
<key>LibraryIdentifier</key>
<string>ios-arm64_x86_64-simulator</string>
<key>LibraryPath</key>
<string>SshLib.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>macos-arm64_x86_64</string>
<key>LibraryPath</key>
<string>SshLib.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>macos</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>ios-arm64</string>
<key>LibraryPath</key>
<string>SshLib.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
</dict>
</array>
<key>CFBundlePackageType</key>
<string>XFWK</string>
<key>XCFrameworkFormatVersion</key>
<string>1.0</string>
</dict>
</plist>
1 change: 1 addition & 0 deletions SshLib.xcframework/ios-arm64/SshLib.framework/Headers
1 change: 1 addition & 0 deletions SshLib.xcframework/ios-arm64/SshLib.framework/Modules
1 change: 1 addition & 0 deletions SshLib.xcframework/ios-arm64/SshLib.framework/Resources
1 change: 1 addition & 0 deletions SshLib.xcframework/ios-arm64/SshLib.framework/SshLib
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

// Objective-C API for talking to the following Go packages
//
// sshlib
//
// File is generated by gomobile bind. Do not edit.
#ifndef __SshLib_FRAMEWORK_H__
#define __SshLib_FRAMEWORK_H__

#include "Sshlib.objc.h"
#include "Universe.objc.h"

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Objective-C API for talking to sshlib Go package.
// gobind -lang=objc sshlib
//
// File is generated by gobind. Do not edit.

#ifndef __Sshlib_H__
#define __Sshlib_H__

@import Foundation;
#include "ref.h"
#include "Universe.objc.h"


FOUNDATION_EXPORT void SshlibInitSSH(NSString* _Nullable sshAddress, NSString* _Nullable socks5Address, NSString* _Nullable sshUser, NSString* _Nullable sshPass);

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Objective-C API for talking to Go package.
// gobind -lang=objc
//
// File is generated by gobind. Do not edit.

#ifndef __Universe_H__
#define __Universe_H__

@import Foundation;
#include "ref.h"

@protocol Universeerror;
@class Universeerror;

@protocol Universeerror <NSObject>
- (NSString* _Nonnull)error;
@end

@class Universeerror;

@interface Universeerror : NSError <goSeqRefInterface, Universeerror> {
}
@property(strong, readonly) _Nonnull id _ref;

- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
- (NSString* _Nonnull)error;
@end

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#ifndef __GO_REF_HDR__
#define __GO_REF_HDR__

#include <Foundation/Foundation.h>

// GoSeqRef is an object tagged with an integer for passing back and
// forth across the language boundary. A GoSeqRef may represent either
// an instance of a Go object, or an Objective-C object passed to Go.
// The explicit allocation of a GoSeqRef is used to pin a Go object
// when it is passed to Objective-C. The Go seq package maintains a
// reference to the Go object in a map keyed by the refnum along with
// a reference count. When the reference count reaches zero, the Go
// seq package will clear the corresponding entry in the map.
@interface GoSeqRef : NSObject {
}
@property(readonly) int32_t refnum;
@property(strong) id obj; // NULL when representing a Go object.

// new GoSeqRef object to proxy a Go object. The refnum must be
// provided from Go side.
- (instancetype)initWithRefnum:(int32_t)refnum obj:(id)obj;

- (int32_t)incNum;

@end

@protocol goSeqRefInterface
-(GoSeqRef*) _ref;
@end

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
framework module "SshLib" {
header "ref.h"
header "Sshlib.objc.h"
header "Universe.objc.h"
header "SshLib.h"

export *
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

// Objective-C API for talking to the following Go packages
//
// sshlib
//
// File is generated by gomobile bind. Do not edit.
#ifndef __SshLib_FRAMEWORK_H__
#define __SshLib_FRAMEWORK_H__

#include "Sshlib.objc.h"
#include "Universe.objc.h"

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Objective-C API for talking to sshlib Go package.
// gobind -lang=objc sshlib
//
// File is generated by gobind. Do not edit.

#ifndef __Sshlib_H__
#define __Sshlib_H__

@import Foundation;
#include "ref.h"
#include "Universe.objc.h"


FOUNDATION_EXPORT void SshlibInitSSH(NSString* _Nullable sshAddress, NSString* _Nullable socks5Address, NSString* _Nullable sshUser, NSString* _Nullable sshPass);

#endif
Loading

0 comments on commit 53da43d

Please sign in to comment.