Skip to content

Commit

Permalink
优化title,跨域说明
Browse files Browse the repository at this point in the history
  • Loading branch information
cnwhy committed Nov 27, 2023
1 parent 1a041a8 commit 6f3f948
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 93 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@

1. 添加配置时必须有`name`字段,修改时`name`字段会被忽略;
2. `GOST`的API是支持跨域的,如碰到跨域问题,检查一下页面和API的协议是否一至
3. 由于`Chrome`安全规则,连接本地`GOST API`(127.0.0.1:xxx 或 localhost:xxx) 需要用[https页面](https://blog.whyoop.com/gost-ui/); 但连接局域网内的`GOST API`只能修改浏览器配置 [设置方式可以参考这篇博文](https://blog.csdn.net/Flywithdawn/article/details/128253604)
37 changes: 21 additions & 16 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect, useCallback } from "react";
import { useState, useEffect, useCallback, useRef } from "react";
import {
Button,
Col,
Expand Down Expand Up @@ -44,40 +44,45 @@ function App() {
const [isDark, setIsDark] = useState(
window.matchMedia("(prefers-color-scheme: dark)").matches
);
const updateConfig = useCallback(() => {
return API.getConfig().then((data) => {
setGostConfig(data);
return data;
});
}, []);

useEffect(() => {
init();
const updateConfig = (reqConfig: any) => {
console.log("reqConfig", reqConfig);
if(reqConfig.url === API.apis.config) return;
const slef = useRef({
updateConfig: () => {
return API.getConfig().then((data) => {
setGostConfig(data);
return data;
});
},
defaultTitle: document.title,
});

useEffect(() => {
init();
const apiUpdate = (reqConfig: any) => {
console.log("reqConfig", reqConfig);
if (reqConfig.url === API.apis.config) return;
return slef.current.updateConfig();
};
configEvent.on("apiUpdate", updateConfig);
configEvent.on("apiUpdate", apiUpdate);
return () => {
configEvent.off("apiUpdate", updateConfig);
configEvent.off("apiUpdate", apiUpdate);
};
}, []);

useEffect(() => {
if (gostInfo) {
updateConfig();
slef.current.updateConfig().then(() => {
document.title = gostInfo.addr.replace(/^(https?:)?\/\//,'');
});
} else {
document.title = slef.current.defaultTitle;
}
}, [gostInfo]);

return (
<Ctx.Provider
value={{
gostConfig,
updateConfig,
updateConfig: slef.current.updateConfig,
logout,
}}
>
Expand Down
204 changes: 127 additions & 77 deletions src/uitls/templates.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
import * as JSONC from "jsonc-parser";
const proxyTypes = "http,http2,socks,socks5,ss,ssu,sni,relay";
const listenerTypes =
"h2,h2c,h3,tls,dtls,ws,wss,grpc,quic,pht,phts,kcp,ssh,mtcp";

const getProxyJson = (
handlerType: string,
listenerType: string,
metadata?: object
) => {
let jsonc = `
{
"name": "service-0",
"addr": ":1080",
"handler": {
"type": "${handlerType}",
// "auth": {
// "username": "user",
// "password": "password"
// }
},
"listener": {
"type": "${listenerType}"
}
}`;
if (metadata) {
const edits = JSONC.modify(jsonc, ["metadata"], metadata, {});
edits.push(...JSONC.modify(jsonc, ["handler", "metadata"], metadata, {}));
edits.push(...JSONC.modify(jsonc, ["listener", "metadata"], metadata, {}));
// edits.push(...JSONC.modify(jsonc, ["name"], undefined, {})) // undefined 可删除
jsonc = JSONC.applyEdits(jsonc, edits);
// jsonc = JSONC.applyEdits(
// jsonc,
// JSONC.modify(jsonc, ["metadata"], metadata, {})
// );
// jsonc = JSONC.applyEdits(
// jsonc,
// JSONC.modify(jsonc, ["handler","metadata"], metadata, {})
// );
// jsonc = JSONC.applyEdits(
// jsonc,
// JSONC.modify(jsonc, ["listener","metadata"], metadata, {})
// );
}

return jsonc;
};

export default {
services: [
{
Expand All @@ -23,6 +71,37 @@ export default {
}
}`,
},
{
label: "反向代理",
cli: "",
json: `
{
"name": "service-0",
"addr": ":80",
"handler": {
"type": "tcp",
"metadata": {
"sniffing": "true"
}
},
"listener": {
"type": "tcp"
},
"forwarder": {
"nodes": [
{
"name": "target-1",
"addr": "www.baidu.com:80",
// "host": "myhost.com",
// "path": "/"
"http": {
"host": "www.baidu.com"
}
}
]
}
}`,
},
{
label: "代理服务",
children: [
Expand All @@ -31,90 +110,61 @@ export default {
children: [
{
label: "http",
json: `
{
"name": "service-0",
"addr": ":1080",
"handler": {
"type": "http",
"auth": {
"username": "user",
"password": "password"
}
},
"listener": {
"type": "tcp"
}
}`,
json: getProxyJson("http", "tcp"),
},
{
label: "https",
json: `
{
"name": "service-0",
"addr": ":1080",
"handler": {
"type": "http",
"auth": {
"username": "user",
"password": "password"
}
},
"listener": {
"type": "tls"
}
}`,
label: "https(http+tsl)",
json: getProxyJson("http", "tls"),
},
{
label: "http+wss",
json: getProxyJson("http", "wss"),
},
{
label: "http2",
json: getProxyJson("http2", "http2"),
},
],
},
{
label: "socks5",
json: `
{
"name": "service-0",
"addr": ":1080",
"handler": {
"type": "socks5",
"auth": {
"username": "user",
"password": "password"
},
"metadata": {
"udp": "true"
}
label: "relay",
children: [
{
label: "relay+tcp",
json: getProxyJson("relay", "tcp"),
},
"listener": {
"type": "tcp",
"metadata": {
"udp": "true"
}
{
label: "relay+tls",
json: getProxyJson("relay", "tls"),
},
"metadata": {
"udp": "true"
}
}`,
{
label: "relay+wss",
json: getProxyJson("relay", "tls"),
},
],
},
{
label: "socks",
children: [
{
label: "socks4",
json: getProxyJson("socks", "tcp"),
},
{
label: "socks5",
json: getProxyJson("socks5", "tcp"),
},
{
label: "socks5(支持udp)",
json: getProxyJson("socks5", "tcp", { udp: "true" }),
},
{
label: "socks5+tls",
json: getProxyJson("socks5", "tls", { notls: "true" }),
},
],
},
],
// json: `
// {
// "name": "service-0",
// "addr": ":1080",
// "handler": {
// "type": "socks5",
// "metadata": {
// "udp": "true"
// }
// },
// "listener": {
// "type": "tcp",
// "metadata": {
// "udp": "true"
// }
// },
// "metadata": {
// "udp": "true"
// }
// }`,
},
{
label: "代理转发",
Expand All @@ -135,7 +185,7 @@ export default {
],
chains: [
{
label: "典型转发",
label: "典型转发链",
cli: "",
json: `
{
Expand Down Expand Up @@ -165,8 +215,8 @@ export default {
}
]
}
`
}
`,
},
],
authers: [
{
Expand Down

0 comments on commit 6f3f948

Please sign in to comment.