Skip to content

Commit

Permalink
Add RedirectService
Browse files Browse the repository at this point in the history
  • Loading branch information
numbaa committed Apr 30, 2024
1 parent 349fb99 commit 05ff7ad
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public class ControlledController {
@Autowired
private OrderService orderService;

@Autowired
private RedirectService redirectService;

@ConnectionEvent(type = ConnectionEventType.Connected)
public void onConnectionConnected(long connectionID) {
log.info("Accepted new connection({})", connectionID);
Expand Down Expand Up @@ -100,12 +103,14 @@ public void onConnectionUnexpectedlyClosed(long connectionID) {
@MessageMapping(proto = LtProto.LoginDevice)
public LtMessage handleLoginDevice(long connectionID, LoginDeviceProto.LoginDevice msg) {
//注意与ControllingController的区别
log.info("Handle LoginDevice(connectionID:{}, deviceID:{})", connectionID, msg.getDeviceId());
var someCondition = false;
if (someCondition) {
log.info("Handle LoginDevice(connectionID:{}, deviceID:{}), version:v{}.{}.{}",
connectionID, msg.getDeviceId(), msg.getVersionMajor(), msg.getVersionMinor(), msg.getVersionPatch());
int versionNum = msg.getVersionMajor() * 1_000_000 + msg.getVersionMinor() * 1_000 + msg.getVersionPatch();
var redirectedAddress = redirectService.redirectControlled(connectionID, versionNum);
if (redirectedAddress != null) {
var redirect = RedirectServerAddressProto.RedirectServerAddress.newBuilder();
redirect.setHost("somehost");
redirect.setPort(1234);
redirect.setHost(redirectedAddress.host());
redirect.setPort(redirectedAddress.port());
return new LtMessage(LtProto.RedirectServerAddress.ID, redirect.build());
}
var ack = LoginDeviceAckProto.LoginDeviceAck.newBuilder();
Expand All @@ -123,7 +128,6 @@ public LtMessage handleLoginDevice(long connectionID, LoginDeviceProto.LoginDevi
return new LtMessage(LtProto.LoginDeviceAck.ID, ack.build());
}

int versionNum = msg.getVersionMajor() * 1_000_000 + msg.getVersionMinor() * 1_000 + msg.getVersionPatch();
boolean success = controlledSessionService.loginDevice(connectionID, msg.getDeviceId(), msg.getAllowControl(), versionNum, msg.getOsType().toString());
if (!success) {
ack.setErrCode(ErrorCodeOuterClass.ErrorCode.LoginDeviceInvalidStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public class ControllingController {
@Autowired
private VersionService versionService;

@Autowired
private RedirectService redirectService;

@ConnectionEvent(type = ConnectionEventType.Connected)
public void onConnectionConnected(long connectionID) {
log.info("Accepted new connection({})", connectionID);
Expand Down Expand Up @@ -126,12 +129,14 @@ public LtMessage handleAllocateDeviceID(long connectionID, AllocateDeviceIDProto
@MessageMapping(proto = LtProto.LoginDevice)
public LtMessage handleLoginDevice(long connectionID, LoginDeviceProto.LoginDevice msg) {
//注意与ControlledController的区别
log.info("Handle LoginDevice(connectionID:{}, deviceID:{})", connectionID, msg.getDeviceId());
var someCondition = false;
if (someCondition) {
log.info("Handle LoginDevice(connectionID:{}, deviceID:{}, version:v{}.{}.{})",
connectionID, msg.getDeviceId(), msg.getVersionMajor(), msg.getVersionMinor(), msg.getVersionPatch());
int versionNum = msg.getVersionMajor() * 1_000_000 + msg.getVersionMinor() * 1_000 + msg.getVersionPatch();
var redirectedAddress = redirectService.redirectControlling(connectionID, versionNum);
if (redirectedAddress != null) {
var redirect = RedirectServerAddressProto.RedirectServerAddress.newBuilder();
redirect.setHost("somehost");
redirect.setPort(1234);
redirect.setHost(redirectedAddress.host());
redirect.setPort(redirectedAddress.port());
return new LtMessage(LtProto.RedirectServerAddress.ID, redirect.build());
}
var ack = LoginDeviceAckProto.LoginDeviceAck.newBuilder();
Expand Down Expand Up @@ -180,7 +185,6 @@ public LtMessage handleLoginDevice(long connectionID, LoginDeviceProto.LoginDevi
ack.setNewCookie(usedID.getCookie());
}
// 走到这里,说明id和cookie都对了
int versionNum = msg.getVersionMajor() * 1_000_000 + msg.getVersionMinor() * 1_000 + msg.getVersionPatch();
boolean success = controllingSessionService.loginDevice(connectionID, msg.getDeviceId(), versionNum, msg.getOsType().toString());
if (!success) {
// 失败暂时有两种可能
Expand Down
41 changes: 41 additions & 0 deletions ltsvr/src/main/java/cn/lanthing/svr/service/RedirectService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2024 Zhennan Tu <zhennan.tu@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package cn.lanthing.svr.service;

public interface RedirectService {

record RedirectedAddress(String host, int port) {}

RedirectedAddress redirectControlled(long connectionID, int version);

RedirectedAddress redirectControlling(long connectionID, int version);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2024 Zhennan Tu <zhennan.tu@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package cn.lanthing.svr.service.impl;

import cn.lanthing.svr.service.RedirectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

public class RedirectServiceImpl implements RedirectService {

@Value("${redirect.controlled.host:#{null}}")
private String controlledHost;

@Value("${redirect.controlled.port:0}")
private int controlledPort;

@Value("${redirect.controlling.host:#{null}}")
private String controllingHost;

@Value("${redirect.controlling.port:0}")
private int controllingPort;

@Override
public RedirectedAddress redirectControlled(long connectionID, int version) {
if (StringUtils.isEmpty(controlledHost) || controlledPort == 0) {
return null;
} else {
return new RedirectedAddress(controlledHost, controlledPort);
}
}

@Override
public RedirectedAddress redirectControlling(long connectionID, int version) {
if (StringUtils.isEmpty(controllingHost) || controllingPort == 0) {
return null;
} else {
return new RedirectedAddress(controllingHost, controllingPort);
}
}
}

0 comments on commit 05ff7ad

Please sign in to comment.