refactor: pac修改

pull/67/head
xiaojunnuo 2020-12-04 11:34:06 +08:00
parent 15ba3e65b5
commit 1b36d5781a
5 changed files with 62 additions and 31 deletions

View File

@ -2,7 +2,12 @@ module.exports = {
name: '梯子', name: '梯子',
enabled: false, enabled: false,
server: { server: {
'ow.docmirror.top/_go_over_wall_': true },
serverDefault: {
'ow-prod.docmirror.top': {
path: 'X2dvX292ZXJfd2FsbF8',
password: 'dev_sidecar_is_666'
}
}, },
targets: { targets: {
'*facebook.com': true '*facebook.com': true

View File

@ -41,20 +41,23 @@ const localApi = {
setting: { setting: {
load () { load () {
const settingPath = _getSettingsPath() const settingPath = _getSettingsPath()
if (!fs.existsSync(settingPath)) { let setting = {}
this.save({}) if (fs.existsSync(settingPath)) {
const file = fs.readFileSync(settingPath)
setting = JSON5.parse(file.toString())
if (setting == null) {
setting = {}
}
} }
const file = fs.readFileSync(settingPath) if (setting.overwall == null) {
const setting = JSON5.parse(file.toString())
if (setting && setting.installTime == null) {
setting.installTime = new Date().getTime()
this.save(setting)
}
if (setting && setting.overwall == null) {
setting.overwall = true setting.overwall = true
this.save(setting)
} }
return setting || {}
if (setting.installTime == null) {
setting.installTime = new Date().getTime()
localApi.setting.save(setting)
}
return setting
}, },
save (setting = {}) { save (setting = {}) {
const settingPath = _getSettingsPath() const settingPath = _getSettingsPath()

View File

@ -24,18 +24,18 @@
<a-form-item label="自定义域名" :label-col="labelCol" :wrapper-col="wrapperCol"> <a-form-item label="自定义域名" :label-col="labelCol" :wrapper-col="wrapperCol">
<div> <div>
<a-row :gutter="10" style=""> <a-row :gutter="10" style="">
<a-col :span="14"> <a-col :span="22">
<span>PAC没有拦截到的域名可以在此处定义</span> <span>PAC没有拦截到的域名可以在此处定义</span>
</a-col> </a-col>
<a-col :span="3"> <a-col :span="2">
<a-button type="primary" icon="plus" @click="addTarget()" /> <a-button type="primary" icon="plus" @click="addTarget()" />
</a-col> </a-col>
</a-row> </a-row>
<a-row :gutter="10" v-for="(item,index) of targets" :key = 'index'> <a-row :gutter="10" v-for="(item,index) of targets" :key = 'index'>
<a-col :span="14"> <a-col :span="22">
<a-input v-model="item.key"></a-input> <a-input v-model="item.key"></a-input>
</a-col> </a-col>
<a-col :span="3"> <a-col :span="2">
<a-button type="danger" icon="minus" @click="deleteTarget(item,index)" /> <a-button type="danger" icon="minus" @click="deleteTarget(item,index)" />
</a-col> </a-col>
</a-row> </a-row>
@ -44,22 +44,28 @@
<a-form-item label="代理服务端" :label-col="labelCol" :wrapper-col="wrapperCol"> <a-form-item label="代理服务端" :label-col="labelCol" :wrapper-col="wrapperCol">
<div> <div>
<a-row :gutter="10" style=""> <a-row :gutter="10" style="">
<a-col :span="14"> <a-col :span="22">
<span>Nginx二层代理服务端配置</span> <span>Nginx二层代理服务端配置</span>
</a-col> </a-col>
<a-col :span="3"> <a-col :span="2">
<a-button type="primary" icon="plus" @click="addServer()" /> <a-button type="primary" icon="plus" @click="addServer()" />
</a-col> </a-col>
</a-row> </a-row>
<a-row :gutter="10" v-for="(item,index) of servers" :key = 'index'> <a-row :gutter="10" v-for="(item,index) of servers" :key = 'index'>
<a-col :span="14"> <a-col :span="8">
<a-input v-model="item.key"></a-input> <a-input addon-before="" placeholder="yourdomain.com" v-model="item.key"></a-input>
</a-col> </a-col>
<a-col :span="3"> <a-col :span="7">
<a-input addon-before="" placeholder="xxxxxx" v-model="item.value.path"/>
</a-col>
<a-col :span="7">
<a-input addon-before="" type="password" placeholder="password" v-model="item.value.password"/>
</a-col>
<a-col :span="2">
<a-button type="danger" icon="minus" @click="deleteServer(item,index)" /> <a-button type="danger" icon="minus" @click="deleteServer(item,index)" />
</a-col> </a-col>
</a-row> </a-row>
<div>您可以在此处配置你自己的服务器地址</div>
</div> </div>
</a-form-item> </a-form-item>
</a-form> </a-form>
@ -144,12 +150,15 @@ export default {
key, value key, value
}) })
} }
if (this.servers.length === 0) {
this.addServer()
}
}, },
deleteServer (item, index) { deleteServer (item, index) {
this.servers.splice(index, 1) this.servers.splice(index, 1)
}, },
addServer () { addServer () {
this.servers.unshift({ key: '', value: true }) this.servers.unshift({ key: '', value: { type: 'path' } })
}, },
saveServer () { saveServer () {
const map = {} const map = {}

View File

@ -15,10 +15,18 @@ function matched (hostname, regexpMap) {
} }
module.exports = function createOverWallIntercept (overWallConfig) { module.exports = function createOverWallIntercept (overWallConfig) {
if (!overWallConfig || overWallConfig.enabled !== true || !overWallConfig.server || Object.keys(overWallConfig.server).length === 0) { if (!overWallConfig || overWallConfig.enabled !== true) {
return null
}
let server = overWallConfig.server
let keys = Object.keys(server)
if (keys.length === 0) {
server = overWallConfig.serverDefault
keys = Object.keys(server)
}
if (keys.length === 0) {
return null return null
} }
const keys = Object.keys(overWallConfig.server)
const regexpMap = matchUtil.domainMapRegexply(overWallConfig.intercepts) const regexpMap = matchUtil.domainMapRegexply(overWallConfig.intercepts)
return { return {
sslConnectInterceptor: (req, cltSocket, head) => { sslConnectInterceptor: (req, cltSocket, head) => {
@ -34,10 +42,10 @@ module.exports = function createOverWallIntercept (overWallConfig) {
if (!matched(hostname, regexpMap)) { if (!matched(hostname, regexpMap)) {
return return
} }
const key = '__over_wall_proxy__' const cacheKey = '__over_wall_proxy__'
let proxyServer = keys[0] let proxyServer = keys[0]
if (RequestCounter && keys.length > 1) { if (RequestCounter && keys.length > 1) {
const count = RequestCounter.getOrCreate(key, keys) const count = RequestCounter.getOrCreate(cacheKey, keys)
if (count.value == null) { if (count.value == null) {
count.doRank() count.doRank()
} }
@ -47,23 +55,29 @@ module.exports = function createOverWallIntercept (overWallConfig) {
count.doCount(count.value) count.doCount(count.value)
proxyServer = count.value proxyServer = count.value
context.requestCount = { context.requestCount = {
key, key: cacheKey,
value: count.value, value: count.value,
count count
} }
} }
} }
const proxyTarget = proxyServer + '/' + hostname + req.url const domain = proxyServer
const path = server[domain].path
const password = server[domain].password
const proxyTarget = domain + '/' + path + '/' + hostname + req.url
// const backup = interceptOpt.backup // const backup = interceptOpt.backup
const proxy = proxyTarget.indexOf('http') === 0 ? proxyTarget : rOptions.protocol + '//' + proxyTarget const proxy = proxyTarget.indexOf('http') === 0 ? proxyTarget : (rOptions.protocol + '//' + proxyTarget)
// eslint-disable-next-line node/no-deprecated-api // eslint-disable-next-line node/no-deprecated-api
const URL = url.parse(proxy) const URL = url.parse(proxy)
rOptions.protocol = URL.protocol rOptions.protocol = URL.protocol
rOptions.hostname = URL.host rOptions.hostname = URL.host
rOptions.host = URL.host rOptions.host = URL.host
rOptions.headers.host = URL.host rOptions.headers.host = URL.host
if (password) {
rOptions.headers.dspassword = password
}
rOptions.path = URL.path rOptions.path = URL.path
if (URL.port == null) { if (URL.port == null) {
rOptions.port = rOptions.protocol === 'https:' ? 443 : 80 rOptions.port = rOptions.protocol === 'https:' ? 443 : 80

View File

@ -47,7 +47,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
reqIncpts.unshift(InsertScriptMiddleware) reqIncpts.unshift(InsertScriptMiddleware)
} }
for (const middleware of middlewares) { for (const middleware of middlewares) {
reqIncpts.unshift(middleware) reqIncpts.push(middleware)
} }
if (reqIncpts && reqIncpts.length > 0) { if (reqIncpts && reqIncpts.length > 0) {
for (const reqIncpt of reqIncpts) { for (const reqIncpt of reqIncpts) {