Skip to content
This repository has been archived by the owner on Jul 21, 2024. It is now read-only.

Commit

Permalink
* fix DeviceSessionSignatureInvalid
Browse files Browse the repository at this point in the history
  • Loading branch information
eritpchy committed Feb 16, 2023
1 parent eb67482 commit cbccbd6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,48 @@ public class AliYunDriverClient {
private OkHttpClient okHttpClient;
public AliYunDriveProperties aliYunDriveProperties;

public Request buildCommonRequestHeader(Request request) {
return request.newBuilder()
.removeHeader("User-Agent")
.addHeader("User-Agent", aliYunDriveProperties.agent)
.removeHeader("authorization")
.addHeader("authorization", "Bearer\t" + aliYunDriveProperties.authorization)
.removeHeader("x-device-id")
.addHeader("x-device-id", aliYunDriveProperties.deviceId)
.removeHeader("x-signature")
.addHeader("x-signature", aliYunDriveProperties.session.signature + "01")
.removeHeader("x-canary")
.addHeader("x-canary", "client=web,app=adrive,version=v3.17.0")
.removeHeader("x-request-id")
.addHeader("x-request-id", UUID.randomUUID().toString())
.build();
}

public AliYunDriverClient(AliYunDriveProperties aliYunDriveProperties) {
try {
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
request = request.newBuilder()
.removeHeader("User-Agent")
.addHeader("User-Agent", aliYunDriveProperties.agent)
.removeHeader("authorization")
.addHeader("authorization", "Bearer\t" + aliYunDriveProperties.authorization)
.addHeader("x-device-id", aliYunDriveProperties.deviceId)
.addHeader("x-signature", aliYunDriveProperties.session.signature + "01")
.addHeader("x-canary", "client=web,app=adrive,version=v3.17.0")
.addHeader("x-request-id", UUID.randomUUID().toString())
.build();
return chain.proceed(request);
Response response = chain.proceed(buildCommonRequestHeader(request));
int code = response.code();
if (code == 400 || code == 401) {
ResponseBody body = response.peekBody(40960);
String res = body.string();
String url = request.url().toString();
if ((!url.endsWith("/renew_session")) && res.contains("DeviceSessionSignatureInvalid")) {
AliYunSessionManager mgr = new AliYunSessionManager(AliYunDriverClient.this);
mgr.updateSession();
return chain.proceed(buildCommonRequestHeader(request));
} else if (res.contains("UserDeviceOffline")) {
AliYunDriverClient.this.aliYunDriveProperties.session.nonce = 0;
AliYunDriverClient.this.aliYunDriveProperties.session.expireTimeSec = 0;
AliYunDriverClient.this.aliYunDriveProperties.save();
LOGGER.error("登录设备过多, 请进入\"登录设备管理\", 退出一些设备。");
return response;
}
}
return response;
}
}).authenticator(new Authenticator() {
@Override
Expand Down Expand Up @@ -86,21 +111,7 @@ public Request authenticate(Route route, Response response) throws IOException {
.removeHeader("authorization")
.header("authorization", accessToken)
.build();
} else if (res.contains("DeviceSessionSignatureInvalid")) {
AliYunSessionManager mgr = new AliYunSessionManager(AliYunDriverClient.this);
mgr.makeKeyPair();
return response.request().newBuilder()
.removeHeader("x-signature")
.header("x-signature", aliYunDriveProperties.session.signature + "01")
.build();
} else if (res.contains("UserDeviceOffline")) {
AliYunDriverClient.this.aliYunDriveProperties.session.nonce = 0;
AliYunDriverClient.this.aliYunDriveProperties.session.expireTimeSec = 0;
AliYunDriverClient.this.aliYunDriveProperties.save();
LOGGER.error("登录设备过多, 请进入\"登录设备管理\", 退出一些设备。");
return null;
}

}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.zxbu.webdavteambition.config.AliYunDriveProperties;

import net.sf.webdav.exceptions.WebdavException;
import net.xdow.aliyundriver.BuildConfig;

import org.bouncycastle.util.encoders.Hex;
import org.slf4j.Logger;
Expand All @@ -22,6 +23,9 @@
import java.util.concurrent.TimeUnit;

public class AliYunSessionManager {

public static final int SIGN_EXPIRED_TIME_SEC = 60 * 5; //5min

private static final Logger LOGGER = LoggerFactory.getLogger(AliYunSessionManager.class);

private final AliYunDriverClient aliYunDriverClient;
Expand Down Expand Up @@ -60,7 +64,7 @@ private void makeSignature(int nonce) {
Sign.SignatureData signatureInfo = Sign.signMessage(dataHash, keyPair, false);
session.signature = Hex.toHexString(signatureInfo.getR()) + Hex.toHexString(signatureInfo.getS());
session.nonce = nonce;
session.expireTimeSec = System.currentTimeMillis() / 1000 + (3600*23);
session.expireTimeSec = System.currentTimeMillis() / 1000 + SIGN_EXPIRED_TIME_SEC;
}

public void updateSession() {
Expand Down Expand Up @@ -100,7 +104,6 @@ public void updateSession() {
this.aliYunDriverClient.aliYunDriveProperties.save();
}


public void start() {
updateSession();
mTaskPool.scheduleAtFixedRate(new Runnable() {
Expand All @@ -112,7 +115,7 @@ public void run() {
}
updateSession();
}
}, 10, 1, TimeUnit.SECONDS);
}, 10, 10, TimeUnit.SECONDS);
}

public void stop() {
Expand Down

0 comments on commit cbccbd6

Please sign in to comment.