commit
b97f075171
@ -0,0 +1,117 @@
|
||||
package com.idormy.sms.forwarder.model.vo;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.idormy.sms.forwarder.sender.SmsHubApiTask;
|
||||
import com.idormy.sms.forwarder.utils.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SmsHubVo implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public SmsHubVo() {
|
||||
}
|
||||
|
||||
public SmsHubVo(Type type, Integer simId, String content, String target) {
|
||||
this.msgId = UUID.randomUUID().toString();
|
||||
if (channel != null) {
|
||||
String simInfo = simId == 2 ? SettingUtil.getAddExtraSim2() : SettingUtil.getAddExtraSim1(); //自定义备注优先
|
||||
if (!simInfo.isEmpty()) {
|
||||
simInfo = "SIM" + simId + ":" + simInfo;
|
||||
} else {
|
||||
simInfo = SimUtil.getSimInfo(simId);
|
||||
}
|
||||
this.channel = simInfo;
|
||||
}
|
||||
this.content = content;
|
||||
this.target = target;
|
||||
this.action = Action.receive.code();
|
||||
this.type = type.code;
|
||||
}
|
||||
|
||||
//唯一id
|
||||
private String msgId;
|
||||
//心跳数据时发送的设备名
|
||||
private String deviceInfo;
|
||||
//卡槽信息
|
||||
private String channel;
|
||||
//消息内容
|
||||
private String content;
|
||||
//错误消息
|
||||
private String errMsg;
|
||||
//手机号(;分隔)或包名
|
||||
private String target;
|
||||
//状态或操作
|
||||
private String action;
|
||||
//消息类型
|
||||
private String type;
|
||||
//时间戳
|
||||
private String ts;
|
||||
//两次交互之间接收到的消息
|
||||
private List<SmsHubVo> children;
|
||||
|
||||
public static SmsHubVo heartbeatInstance() {
|
||||
SmsHubVo smsHubVo = new SmsHubVo();
|
||||
HashMap<String, String> deviInfoMap = getDevInfoMap(false);
|
||||
smsHubVo.setDeviceInfo(JSON.toJSONString(deviInfoMap));
|
||||
smsHubVo.setChannel("SIM1:" + SimUtil.getSimInfo(1) + SettingUtil.getAddExtraSim1() + ";SIM2:" + SimUtil.getSimInfo(2) + SettingUtil.getAddExtraSim2());
|
||||
smsHubVo.setTs(Long.toString(System.currentTimeMillis()));
|
||||
smsHubVo.setAction(SmsHubVo.Action.heartbeat.code());
|
||||
return smsHubVo;
|
||||
}
|
||||
|
||||
private static Map<String, Object> cache = new ConcurrentHashMap<>();
|
||||
|
||||
@NonNull
|
||||
public static HashMap<String, String> getDevInfoMap(boolean reflush) {
|
||||
String key = "deviceInfo";
|
||||
if (reflush || !cache.containsKey(key)) {
|
||||
HashMap<String, String> deviInfoMap = new HashMap<>();
|
||||
deviInfoMap.put("mark", SettingUtil.getAddExtraDeviceMark());
|
||||
deviInfoMap.put("simOperatorName", PhoneUtils.getSimOperatorName());
|
||||
deviInfoMap.put("phoneNumber", PhoneUtils.getPhoneNumber());
|
||||
deviInfoMap.put("imei", PhoneUtils.getIMEI());
|
||||
deviInfoMap.put("SDKVersion", PhoneUtils.getSDKVersion() + "");
|
||||
deviInfoMap.put("Version", SettingUtil.getVersionName());
|
||||
deviInfoMap.put("heartbeat", SmsHubApiTask.DELAY_SECONDS + "");
|
||||
cache.put(key, deviInfoMap);
|
||||
return deviInfoMap;
|
||||
}
|
||||
return (HashMap<String, String>) cache.get(key);
|
||||
}
|
||||
|
||||
public enum Action {
|
||||
send("0"), receive("1"), suessces("2"), failure("3"), heartbeat("-1");
|
||||
|
||||
Action(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
private final String code;
|
||||
|
||||
public String code() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
app("app"), phone("phone"), sms("sms"), battery("battery");
|
||||
|
||||
Type(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
private final String code;
|
||||
|
||||
public String code() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,172 @@
|
||||
package com.idormy.sms.forwarder.receiver;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.util.IOUtils;
|
||||
import com.idormy.sms.forwarder.CloneActivity;
|
||||
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
|
||||
import com.idormy.sms.forwarder.utils.SmsHubActionHandler;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.MultipartConfigElement;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.annotation.MultipartConfig;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.*;
|
||||
|
||||
@WebServlet
|
||||
@MultipartConfig
|
||||
public class BaseServlet extends HttpServlet {
|
||||
|
||||
public static final int BUFFER_SIZE = 1 << 12;
|
||||
public static final String CLONE_PATH = "/";
|
||||
public static final String SMSHUB_PATH = "/send_api";
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final String TAG = "BaseServlet";
|
||||
private static final SmsHubActionHandler.SmsHubMode smsHubMode = SmsHubActionHandler.SmsHubMode.server;
|
||||
|
||||
public BaseServlet(String path, Context context) {
|
||||
this.path = path;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
private final String path;
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private final Context context;
|
||||
|
||||
public static void addServlet(Server jettyServer, Context context) {
|
||||
ServletContextHandler contextHandler = new ServletContextHandler();
|
||||
addHolder(contextHandler, new BaseServlet(BaseServlet.CLONE_PATH, context));
|
||||
addHolder(contextHandler, new BaseServlet(BaseServlet.SMSHUB_PATH, context));
|
||||
// addholder(contextHandler, new BaseServlet("/", context));
|
||||
jettyServer.setHandler(contextHandler);
|
||||
}
|
||||
|
||||
public static String read(Reader reader) throws IOException {
|
||||
char[] buffer = new char[BUFFER_SIZE];
|
||||
int size;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while ((size = reader.read(buffer)) != -1) {
|
||||
char[] chars = new char[size];
|
||||
System.arraycopy(buffer, 0, chars, 0, size);
|
||||
sb.append(chars);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static void addHolder(ServletContextHandler servletContextHandler, BaseServlet baseServlet) {
|
||||
ServletHolder servletHolder = new ServletHolder(baseServlet);
|
||||
servletHolder.getRegistration().setMultipartConfig(new MultipartConfigElement(baseServlet.getContext().getCacheDir().getAbsolutePath() + File.pathSeparator + "jettyServer"));
|
||||
servletContextHandler.addServlet(servletHolder, baseServlet.getPath());
|
||||
}
|
||||
|
||||
public Context getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||
if (CLONE_PATH.equals(path)) {
|
||||
clone(req, resp);
|
||||
} else if (SMSHUB_PATH.equals(path)) {
|
||||
send_api(req, resp);
|
||||
} else {
|
||||
notFound(req, resp);
|
||||
}
|
||||
}
|
||||
|
||||
private void notFound(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||
PrintWriter writer = resp.getWriter();
|
||||
try {
|
||||
String text = "NOT FOUND";
|
||||
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
writer.println(text);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
IOUtils.close(writer);
|
||||
}
|
||||
}
|
||||
|
||||
private void send_api(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||
resp.setCharacterEncoding("utf-8");
|
||||
PrintWriter writer = resp.getWriter();
|
||||
BufferedReader reader = req.getReader();
|
||||
try {
|
||||
String read = read(reader);
|
||||
Log.i(TAG, "请求内容:" + read);
|
||||
List<SmsHubVo> smsHubVos = JSON.parseArray(read, SmsHubVo.class);
|
||||
if (smsHubVos.size() == 1 && SmsHubVo.Action.heartbeat.code().equals(smsHubVos.get(0).getAction())) {
|
||||
smsHubVos.clear();
|
||||
SmsHubVo smsHubVo = SmsHubVo.heartbeatInstance();
|
||||
smsHubVos.add(smsHubVo);
|
||||
List<SmsHubVo> data = SmsHubActionHandler.getData(smsHubMode);
|
||||
if (data != null && data.size() > 0) {
|
||||
smsHubVo.setChildren(data);
|
||||
}
|
||||
} else {
|
||||
for (SmsHubVo vo : smsHubVos) {
|
||||
SmsHubActionHandler.handle(TAG, vo);
|
||||
}
|
||||
List<SmsHubVo> data = SmsHubActionHandler.getData(smsHubMode);
|
||||
if (data != null && data.size() > 0) {
|
||||
SmsHubVo smsHubVo = SmsHubVo.heartbeatInstance();
|
||||
smsHubVo.setChildren(data);
|
||||
smsHubVos.add(smsHubVo);
|
||||
}
|
||||
}
|
||||
resp.setContentType("application/json;charset=utf-8");
|
||||
String text = JSON.toJSONString(smsHubVos);
|
||||
writer.println(text);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
printErrMsg(resp, writer, e);
|
||||
} finally {
|
||||
IOUtils.close(reader);
|
||||
IOUtils.close(writer);
|
||||
}
|
||||
}
|
||||
|
||||
private void printErrMsg(HttpServletResponse resp, PrintWriter writer, Exception e) {
|
||||
String text = "服务器内部错误:" + e.getMessage();
|
||||
Log.e(TAG, text);
|
||||
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
writer.println(text);
|
||||
}
|
||||
|
||||
private void clone(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||
File file = context.getDatabasePath(CloneActivity.DATABASE_NAME);
|
||||
resp.addHeader("Content-Disposition", "attachment;filename=" + CloneActivity.DATABASE_NAME);
|
||||
ServletOutputStream outputStream = resp.getOutputStream();
|
||||
InputStream inputStream = new FileInputStream(file);
|
||||
try {
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int size;
|
||||
while ((size = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, size);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
String text = "服务器内部错误:" + e.getMessage();
|
||||
Log.e(TAG, text);
|
||||
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
} finally {
|
||||
IOUtils.close(inputStream);
|
||||
IOUtils.close(outputStream);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
package com.idormy.sms.forwarder.sender;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.idormy.sms.forwarder.R;
|
||||
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
|
||||
import com.idormy.sms.forwarder.receiver.BaseServlet;
|
||||
import com.idormy.sms.forwarder.utils.*;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
|
||||
|
||||
public class HttpServer {
|
||||
private static Boolean hasInit = false;
|
||||
private static Server jettyServer;
|
||||
private static int port = 5000;
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static Context context;
|
||||
private static long ts = 0L;
|
||||
|
||||
|
||||
@SuppressLint("HandlerLeak")
|
||||
public static void init(Context context) {
|
||||
//noinspection SynchronizeOnNonFinalField
|
||||
synchronized (hasInit) {
|
||||
if (hasInit) return;
|
||||
|
||||
hasInit = true;
|
||||
HttpServer.context = context;
|
||||
SmsHubActionHandler.init(context);
|
||||
jettyServer = new Server(port);
|
||||
BaseServlet.addServlet(jettyServer, context);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the Jetty is running
|
||||
* boolean - true when server is running/starting/stopping, false otherwise
|
||||
*/
|
||||
public synchronized static Boolean asRunning() {
|
||||
if (jettyServer != null) {
|
||||
return jettyServer.isRunning() && !jettyServer.isStopping();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public synchronized static boolean update() {
|
||||
if (!asRunning() && NetUtil.NETWORK_WIFI != NetUtil.getNetWorkStatus()) {
|
||||
Toast.makeText(context, R.string.no_wifi_network, Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
}
|
||||
long l = System.currentTimeMillis();
|
||||
if (l - ts < 3000 && asRunning()) {
|
||||
Toast.makeText(context, "点击启动后请等待3秒", Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
}
|
||||
if (asRunning().equals(SettingUtil.getSwitchEnableHttpServer())) {
|
||||
return false;
|
||||
}
|
||||
if (SettingUtil.getSwitchEnableHttpServer()) {
|
||||
SmsHubVo.getDevInfoMap(true);
|
||||
start();
|
||||
ts = System.currentTimeMillis();
|
||||
Toast.makeText(context, R.string.server_has_started, Toast.LENGTH_SHORT).show();
|
||||
return true;
|
||||
} else {
|
||||
stop();
|
||||
Toast.makeText(context, R.string.server_has_stopped, Toast.LENGTH_SHORT).show();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if Jetty is stopping
|
||||
* boolean - True when server is stopping
|
||||
*/
|
||||
private synchronized static Boolean asStopp() {
|
||||
if (jettyServer != null) {
|
||||
return !(jettyServer.isRunning() || jettyServer.isStopping());
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static void start() {
|
||||
stop();
|
||||
Log.i("HttpServer", "start");
|
||||
//new Thread(() -> {
|
||||
try {
|
||||
//Start Jetty
|
||||
jettyServer.start();
|
||||
//jettyServer.join();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//}).start();
|
||||
}
|
||||
|
||||
|
||||
private static void stop() {
|
||||
if (Boolean.FALSE.equals(asStopp())) {
|
||||
try {
|
||||
if (jettyServer != null) {
|
||||
jettyServer.stop();
|
||||
// jettyServer = new Server(port);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.idormy.sms.forwarder.utils;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Lamda {
|
||||
public interface Consumer<T> extends Func<T, T> {
|
||||
void accept(T t) throws Exception;
|
||||
|
||||
default T execute(T t) throws Exception {
|
||||
accept(t);
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
public interface Func<T, R> {
|
||||
R execute(T t) throws Exception;
|
||||
|
||||
default R executeThrowRunTimeExcp(T t) {
|
||||
try {
|
||||
return execute(t);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
default R executeIgnoreExcp(T t) {
|
||||
try {
|
||||
return execute(t);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
default <E> Func<T, E> andThen(Func<? super R, ? extends E> after) {
|
||||
Objects.requireNonNull(after);
|
||||
return (T t) -> after.execute(execute(t));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,127 @@
|
||||
package com.idormy.sms.forwarder.utils;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.idormy.sms.forwarder.model.LogModel;
|
||||
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class SmsHubActionHandler {
|
||||
public static final long RULE_ID = -999L;
|
||||
private static Boolean hasInit = false;
|
||||
|
||||
private static ConcurrentHashMap<SmsHubMode, List<SmsHubVo>> cache;
|
||||
|
||||
public enum SmsHubMode {
|
||||
server, client
|
||||
}
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static Context context;
|
||||
|
||||
@SuppressLint("HandlerLeak")
|
||||
public static void init(Context context) {
|
||||
//noinspection SynchronizeOnNonFinalField
|
||||
synchronized (hasInit) {
|
||||
if (hasInit) return;
|
||||
|
||||
hasInit = true;
|
||||
SmsHubActionHandler.context = context;
|
||||
cache = new ConcurrentHashMap<>();
|
||||
for (SmsHubMode smsHubMode : SmsHubMode.values()) {
|
||||
cache.put(smsHubMode, new ArrayList<>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized int size(SmsHubMode smsHubMode) {
|
||||
return Objects.requireNonNull(cache.get(smsHubMode)).size();
|
||||
}
|
||||
|
||||
public static synchronized List<SmsHubVo> getData(SmsHubMode smsHubMode) {
|
||||
List<SmsHubVo> smsHubVoList = cache.get(smsHubMode);
|
||||
if (smsHubVoList.size() > 0) {
|
||||
cache.put(smsHubMode, new ArrayList<>());
|
||||
return smsHubVoList;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void putData(SmsHubMode smsHubMode, SmsHubVo... smsHubVos) {
|
||||
if (isEnable(smsHubMode)) {
|
||||
Objects.requireNonNull(cache.get(smsHubMode)).addAll(Arrays.asList(smsHubVos));
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void putData(SmsHubVo... smsHubVos) {
|
||||
for (SmsHubMode smsHubMode : SmsHubMode.values()) {
|
||||
putData(smsHubMode, smsHubVos);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isEnable(SmsHubMode smsHubMode) {
|
||||
boolean enable = false;
|
||||
if (smsHubMode == SmsHubMode.client) {
|
||||
enable = SettingUtil.getSwitchEnableSmsHubApi();
|
||||
} else if (smsHubMode == SmsHubMode.server) {
|
||||
enable = SettingUtil.getSwitchEnableHttpServer();
|
||||
}
|
||||
return enable;
|
||||
}
|
||||
|
||||
public static void handle(String tag, SmsHubVo vo) {
|
||||
String action = vo.getAction();
|
||||
if (SmsHubVo.Action.send.code().equals(action)) {
|
||||
send(tag, vo);
|
||||
} else {
|
||||
String errMsg = "暂不支持的action:" + action;
|
||||
vo.setErrMsg(errMsg);
|
||||
vo.setAction(SmsHubVo.Action.failure.code());
|
||||
}
|
||||
vo.setTs(Long.toString(System.currentTimeMillis()));
|
||||
}
|
||||
|
||||
public static void send(String tag, SmsHubVo vo) {
|
||||
boolean failure = true;
|
||||
String msg = "";
|
||||
Long logId = null;
|
||||
try {
|
||||
if (SmsHubVo.Action.send.code().equals(vo.getAction())) {
|
||||
vo.setType(SmsHubVo.Type.sms.code());
|
||||
logId = LogUtil.addLog(new LogModel(vo.getType(), vo.getTarget(), vo.getContent(), "SIM" + vo.getChannel(), RULE_ID));
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
|
||||
int simId = Integer.parseInt(vo.getChannel());
|
||||
vo.setChannel("SIM" + simId);
|
||||
msg = SmsUtil.sendSms(SimUtil.getSubscriptionIdBySimId(simId), vo.getTarget(), vo.getContent());
|
||||
if (msg == null) {
|
||||
failure = false;
|
||||
HttpUtil.Toast(tag, "短信发送成功");
|
||||
Log.i(tag, "短信发送成功");
|
||||
vo.setAction(SmsHubVo.Action.suessces.code());
|
||||
LogUtil.updateLog(logId, 2, SmsHubVo.Action.suessces.code());
|
||||
}
|
||||
} else {
|
||||
msg = "api<22";
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
msg += e.getMessage();
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (failure) {
|
||||
msg = "短信发送失败:" + msg;
|
||||
HttpUtil.Toast(tag, msg);
|
||||
Log.i(tag, msg);
|
||||
vo.setAction(SmsHubVo.Action.failure.code());
|
||||
vo.setErrMsg(msg);
|
||||
if (logId != null) {
|
||||
LogUtil.updateLog(logId, 0, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue