mirror of
https://github.com/MatsuriDayo/NekoBoxForAndroid.git
synced 2025-12-19 22:50:05 +08:00
ImplementationofCodeImprovements
I implemented validation in the setVersion and setPassword methods. The password is now stored as a character array ***(char[])*** instead of a String, in line with best practices I also implemented getVersion and getPassword methods so that access to sensitive data is controlled and secure.
This commit is contained in:
parent
15055d9cb9
commit
81e53f682e
@ -1,43 +1,48 @@
|
|||||||
package moe.matsuri.nb4a.proxy.shadowtls;
|
package moe.matsuri.nb4a.proxy.shadowtls;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import com.esotericsoftware.kryo.io.ByteBufferInput;
|
import com.esotericsoftware.kryo.io.ByteBufferInput;
|
||||||
import com.esotericsoftware.kryo.io.ByteBufferOutput;
|
import com.esotericsoftware.kryo.io.ByteBufferOutput;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import io.nekohasekai.sagernet.fmt.KryoConverters;
|
import io.nekohasekai.sagernet.fmt.KryoConverters;
|
||||||
import io.nekohasekai.sagernet.fmt.v2ray.StandardV2RayBean;
|
import io.nekohasekai.sagernet.fmt.v2ray.StandardV2RayBean;
|
||||||
|
|
||||||
public class ShadowTLSBean extends StandardV2RayBean {
|
public class ShadowTLSBean extends StandardV2RayBean {
|
||||||
|
|
||||||
public Integer version;
|
private int version = 3; // Use int instead of Integer
|
||||||
public String password;
|
private char[] password; // Use char[] for security
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initializeDefaultValues() {
|
public void initializeDefaultValues() {
|
||||||
super.initializeDefaultValues();
|
super.initializeDefaultValues();
|
||||||
|
|
||||||
security = "tls";
|
security = "tls";
|
||||||
if (version == null) version = 3;
|
password = new char[0];
|
||||||
if (password == null) password = "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void serialize(ByteBufferOutput output) {
|
public void serialize(ByteBufferOutput output) {
|
||||||
|
try {
|
||||||
output.writeInt(0);
|
output.writeInt(0);
|
||||||
super.serialize(output);
|
super.serialize(output);
|
||||||
output.writeInt(version);
|
output.writeInt(version);
|
||||||
output.writeString(password);
|
output.writeString(new String(password)); // Convert char[] to String
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Handles exceptions during serialization
|
||||||
|
throw new RuntimeException("Error serializing ShadowTLSBean", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deserialize(ByteBufferInput input) {
|
public void deserialize(ByteBufferInput input) {
|
||||||
|
try {
|
||||||
int version_ = input.readInt();
|
int version_ = input.readInt();
|
||||||
super.deserialize(input);
|
super.deserialize(input);
|
||||||
version = input.readInt();
|
version = input.readInt();
|
||||||
password = input.readString();
|
password = input.readString().toCharArray(); // Convert String to char[]
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Handles exceptions during deserialization
|
||||||
|
throw new RuntimeException("Error deserializing ShadowTLSBean", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@ -46,6 +51,28 @@ public class ShadowTLSBean extends StandardV2RayBean {
|
|||||||
return KryoConverters.deserialize(new ShadowTLSBean(), KryoConverters.serialize(this));
|
return KryoConverters.deserialize(new ShadowTLSBean(), KryoConverters.serialize(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getVersion() {
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVersion(int version) {
|
||||||
|
if (version < 0) {
|
||||||
|
throw new IllegalArgumentException("The version cannot be negative.");
|
||||||
|
}
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return new String(password); // Returns the password as a String
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
if (password == null || password.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("The password cannot be null or empty.");
|
||||||
|
}
|
||||||
|
this.password = password.toCharArray(); // Store as char[]
|
||||||
|
}
|
||||||
|
|
||||||
public static final Creator<ShadowTLSBean> CREATOR = new CREATOR<ShadowTLSBean>() {
|
public static final Creator<ShadowTLSBean> CREATOR = new CREATOR<ShadowTLSBean>() {
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user