Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.7
// swift-tools-version: 5.10

import PackageDescription

Expand Down
18 changes: 11 additions & 7 deletions Sources/Tun2SocksKit/Tunnel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import HevSocks5Tunnel
import Tun2SocksKitC

public enum Socks5Tunnel {

private static var tunnelFileDescriptor: Int32? {
var ctlInfo = ctl_info()
withUnsafeMutablePointer(to: &ctlInfo.ctl_name) {
$0.withMemoryRebound(to: CChar.self, capacity: MemoryLayout.size(ofValue: $0.pointee)) {
_ = strcpy($0, "com.apple.net.utun_control")
}
}
for fd: Int32 in 0 ... 1024 {
for fd: Int32 in 0...1024 {
var addr = sockaddr_ctl()
var ret: Int32 = -1
var len = socklen_t(MemoryLayout.size(ofValue: addr))
Expand All @@ -36,7 +37,7 @@ public enum Socks5Tunnel {
}

private static var interfaceName: String? {
guard let tunnelFileDescriptor = tunnelFileDescriptor else {
guard let tunnelFileDescriptor = self.tunnelFileDescriptor else {
return nil
}
var buffer = [UInt8](repeating: 0, count: Int(IFNAMSIZ))
Expand All @@ -60,14 +61,17 @@ public enum Socks5Tunnel {
}
}

@discardableResult
public static func run(withConfig filePath: String) -> Int32 {
guard let fileDescriptor = tunnelFileDescriptor else {
public static func run(withConfig filePath: String, completionHandler: @escaping (Int32) -> ()) {
guard let fileDescriptor = self.tunnelFileDescriptor else {
fatalError("Get tunnel file descriptor failed.")
}
return hev_socks5_tunnel_main(filePath.cString(using: .utf8), fileDescriptor)
}

DispatchQueue.global(qos: .userInitiated).async { [completionHandler] () in
let code = hev_socks5_tunnel_main(filePath.cString(using: .utf8), fileDescriptor)
completionHandler(code)
}
}

public static func quit() {
hev_socks5_tunnel_quit()
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/Tun2SocksKitC/Tun2SocksKitC.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#ifndef Tun2SocksKitC_H
#define Tun2SocksKitC_H

#include <stdint.h>
#include <stdint.h> // For uint32_t, uint16_t
#include <sys/types.h> // For u_int32_t, u_int16_t, u_char

#define CTLIOCGINFO 0xc0644e03UL

struct ctl_info {
u_int32_t ctl_id;
char ctl_name[96];
};

struct sockaddr_ctl {
u_char sc_len;
u_char sc_family;
Expand Down