feat: add close-server-by-netstat

This commit is contained in:
huhuang03 2025-07-02 23:42:52 +08:00
commit 4f7bb266a4
4 changed files with 57 additions and 17 deletions

View file

@ -1,5 +1,5 @@
package com.soug.mm.base.config
object PortConfig {
const val HTTP_PORT = 30018
const val HTTP_PORT = 30069
}

View file

@ -1,23 +1,15 @@
set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]
http_port := "30069"
start_server: push-server close-server-if-need
start_server: push-server close-server-by-netstat
adb shell "nohup sh -c 'CLASSPATH=/data/local/tmp/souls app_process / com.genymobile.scrcpy.Server 3.3.1' > /data/local/tmp/souls.log 2>&1 &"
push-server:
./gradlew assembleRelease
adb push ./server/build/outputs/apk/release/server-release-unsigned.apk /data/local/tmp/souls
close-server-if-need:
#netstat: /proc/kshrink_lruvecd_status: Permission denied
#netstat: /proc/voocphy_batt_fake_temp: Permission denied
#tcp6 0 0 [::]:30018 [::]:* LISTEN 28394/app_process
#tcp6 0 0 ::1:44134 ::1:30018 TIME_WAIT -
#tcp6 0 0 ::1:39992 ::1:30018 TIME_WAIT -
#OP5A15L1:/data/local/tmp $ netstat -tulpn | grep 30018
adb shell "for pid_path in /proc/[0-9]*; do \
pid=$${pid_path#/proc/}; \
if grep -q ':7572' /proc/$$pid/net/tcp 2>/dev/null; then \
kill -9 $$pid; \
echo killed $$pid; \
fi; \
done"
echo:
adb shell "curl http://localhost:{{http_port}}"
close-server-by-netstat:
python script/close_server_by_netstat.py {{http_port}}

View file

@ -0,0 +1,48 @@
import subprocess
import re
import sys
def run_adb_shell(cmd: str) -> str:
"""运行 adb shell 命令,并返回 stdout"""
result = subprocess.run(["adb", "shell", cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.stderr:
print(f"[stderr] {result.stderr.strip()}")
return result.stdout.strip()
def find_pid_by_port(port: int) -> str | None:
"""通过 netstat 查找监听指定端口的 PID"""
print(f"🔍 查找端口 {port} 对应的进程 PID ...")
output = run_adb_shell("netstat -tulpn 2>/dev/null")
for line in output.splitlines():
if f":{port}" in line:
print(f"🧾 匹配行: {line}")
match = re.search(r"\s(\d+)/", line)
if match:
pid = match.group(1)
print(f"✅ 找到 PID: {pid}")
return pid
print(f"❌ 没有进程监听端口 {port}")
return None
def kill_pid(pid: str):
"""kill 远程 Android 上的进程"""
print(f"💥 kill -9 {pid}")
run_adb_shell(f"kill -9 {pid}")
def main():
if len(sys.argv) != 2:
print("用法: python close_server.py <port>")
sys.exit(1)
port = int(sys.argv[1])
pid = find_pid_by_port(port)
if pid:
kill_pid(pid)
if __name__ == "__main__":
main()

View file

@ -53,7 +53,7 @@ object Server {
embeddedServer(Netty, PortConfig.HTTP_PORT) {
routing {
get("/") {
call.respondText("Hello, world!1", ContentType.Text.Html)
call.respondText("Hello, world!2\n", ContentType.Text.Html)
}
}
}.start(wait = true)