refactor: pac修改

pull/67/head
xiaojunnuo 4 years ago
parent 15ba3e65b5
commit 1b36d5781a

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

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

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

@ -15,10 +15,18 @@ function matched (hostname, regexpMap) {
}
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
}
const keys = Object.keys(overWallConfig.server)
const regexpMap = matchUtil.domainMapRegexply(overWallConfig.intercepts)
return {
sslConnectInterceptor: (req, cltSocket, head) => {
@ -34,10 +42,10 @@ module.exports = function createOverWallIntercept (overWallConfig) {
if (!matched(hostname, regexpMap)) {
return
}
const key = '__over_wall_proxy__'
const cacheKey = '__over_wall_proxy__'
let proxyServer = keys[0]
if (RequestCounter && keys.length > 1) {
const count = RequestCounter.getOrCreate(key, keys)
const count = RequestCounter.getOrCreate(cacheKey, keys)
if (count.value == null) {
count.doRank()
}
@ -47,23 +55,29 @@ module.exports = function createOverWallIntercept (overWallConfig) {
count.doCount(count.value)
proxyServer = count.value
context.requestCount = {
key,
key: cacheKey,
value: count.value,
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 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
const URL = url.parse(proxy)
rOptions.protocol = URL.protocol
rOptions.hostname = URL.host
rOptions.host = URL.host
rOptions.headers.host = URL.host
if (password) {
rOptions.headers.dspassword = password
}
rOptions.path = URL.path
if (URL.port == null) {
rOptions.port = rOptions.protocol === 'https:' ? 443 : 80

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

Loading…
Cancel
Save