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:
Felipe 2024-10-26 21:34:21 +00:00 committed by GitHub
parent 15055d9cb9
commit 81e53f682e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,43 +1,48 @@
package moe.matsuri.nb4a.proxy.shadowtls;
import androidx.annotation.NonNull;
import com.esotericsoftware.kryo.io.ByteBufferInput;
import com.esotericsoftware.kryo.io.ByteBufferOutput;
import org.jetbrains.annotations.NotNull;
import io.nekohasekai.sagernet.fmt.KryoConverters;
import io.nekohasekai.sagernet.fmt.v2ray.StandardV2RayBean;
public class ShadowTLSBean extends StandardV2RayBean {
public Integer version;
public String password;
private int version = 3; // Use int instead of Integer
private char[] password; // Use char[] for security
@Override
public void initializeDefaultValues() {
super.initializeDefaultValues();
security = "tls";
if (version == null) version = 3;
if (password == null) password = "";
password = new char[0];
}
@Override
public void serialize(ByteBufferOutput output) {
try {
output.writeInt(0);
super.serialize(output);
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
public void deserialize(ByteBufferInput input) {
try {
int version_ = input.readInt();
super.deserialize(input);
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
@ -46,6 +51,28 @@ public class ShadowTLSBean extends StandardV2RayBean {
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>() {
@NonNull
@Override