Skip to content

Commit

Permalink
support hysteria2
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Sep 4, 2023
1 parent eab03de commit ea65417
Show file tree
Hide file tree
Showing 16 changed files with 689 additions and 123 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/io/nekohasekai/sagernet/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ object Key {
const val SERVER_METHOD = "serverMethod"
const val SERVER_PASSWORD1 = "serverPassword1"

const val PROTOCOL_VERSION = "protocolVersion"

const val SERVER_PROTOCOL = "serverProtocol"
const val SERVER_OBFS = "serverObfs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ object DataStore : OnPreferenceDataStoreChangeListener {
var serverDisableMtuDiscovery by profileCacheStore.boolean(Key.SERVER_DISABLE_MTU_DISCOVERY)
var serverHopInterval by profileCacheStore.stringToInt(Key.SERVER_HOP_INTERVAL) { 10 }

var serverProtocolVersion by profileCacheStore.stringToInt(Key.SERVER_PROTOCOL)
var protocolVersion by profileCacheStore.stringToInt(Key.PROTOCOL_VERSION)

var serverProtocolInt by profileCacheStore.stringToInt(Key.SERVER_PROTOCOL)
var serverPrivateKey by profileCacheStore.string(Key.SERVER_PRIVATE_KEY)
var serverInsecureConcurrency by profileCacheStore.stringToInt(Key.SERVER_INSECURE_CONCURRENCY)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ fun buildConfig(
buildSingBoxOutboundStandardV2RayBean(bean).asMap()

is HysteriaBean ->
buildSingBoxOutboundHysteriaBean(bean).asMap()
buildSingBoxOutboundHysteriaBean(bean)

is TuicBean ->
buildSingBoxOutboundTuicBean(bean).asMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,17 @@
import kotlin.text.StringsKt;

public class HysteriaBean extends AbstractBean {
public Integer protocolVersion;

public static final int TYPE_NONE = 0;
public static final int TYPE_STRING = 1;
public static final int TYPE_BASE64 = 2;

public Integer authPayloadType;
public String authPayload;

public static final int PROTOCOL_UDP = 0;
public static final int PROTOCOL_FAKETCP = 1;
public static final int PROTOCOL_WECHAT_VIDEO = 2;
// Use serverPorts instead of serverPort
public String serverPorts;

public Integer protocol;
// HY1 & 2

public String authPayload;
public String obfuscation;
public String sni;
public String alpn;
public String caText;

public Integer uploadMbps;
public Integer downloadMbps;
public Boolean allowInsecure;
Expand All @@ -40,7 +32,19 @@ public class HysteriaBean extends AbstractBean {
public Boolean disableMtuDiscovery;
public Integer hopInterval;

public String serverPorts;
// HY1

public String alpn;

public static final int TYPE_NONE = 0;
public static final int TYPE_STRING = 1;
public static final int TYPE_BASE64 = 2;
public Integer authPayloadType;

public static final int PROTOCOL_UDP = 0;
public static final int PROTOCOL_FAKETCP = 1;
public static final int PROTOCOL_WECHAT_VIDEO = 2;
public Integer protocol;

@Override
public boolean canMapping() {
Expand All @@ -50,18 +54,25 @@ public boolean canMapping() {
@Override
public void initializeDefaultValues() {
super.initializeDefaultValues();
if (protocolVersion == null) protocolVersion = 2;

if (authPayloadType == null) authPayloadType = TYPE_NONE;
if (authPayload == null) authPayload = "";
if (protocol == null) protocol = PROTOCOL_UDP;
if (obfuscation == null) obfuscation = "";
if (sni == null) sni = "";
if (alpn == null) alpn = "";
if (caText == null) caText = "";

if (uploadMbps == null) uploadMbps = 10;
if (downloadMbps == null) downloadMbps = 50;
if (allowInsecure == null) allowInsecure = false;

if (protocolVersion == 1) {
if (uploadMbps == null) uploadMbps = 10;
if (downloadMbps == null) downloadMbps = 50;
} else {
if (uploadMbps == null) uploadMbps = 0;
if (downloadMbps == null) downloadMbps = 0;
}

if (streamReceiveWindow == null) streamReceiveWindow = 0;
if (connectionReceiveWindow == null) connectionReceiveWindow = 0;
if (disableMtuDiscovery == null) disableMtuDiscovery = false;
Expand All @@ -71,8 +82,11 @@ public void initializeDefaultValues() {

@Override
public void serialize(ByteBufferOutput output) {
output.writeInt(6);
output.writeInt(7);
super.serialize(output);

output.writeInt(protocolVersion);

output.writeInt(authPayloadType);
output.writeString(authPayload);
output.writeInt(protocol);
Expand All @@ -90,13 +104,17 @@ public void serialize(ByteBufferOutput output) {
output.writeBoolean(disableMtuDiscovery);
output.writeInt(hopInterval);
output.writeString(serverPorts);

}

@Override
public void deserialize(ByteBufferInput input) {
int version = input.readInt();
super.deserialize(input);
if (version >= 7) {
protocolVersion = input.readInt();
} else {
protocolVersion = 1;
}
authPayloadType = input.readInt();
authPayload = input.readString();
if (version >= 3) {
Expand Down
Loading

0 comments on commit ea65417

Please sign in to comment.