Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
browser-backend
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
browser-backend
Commits
4d35f9a8
Commit
4d35f9a8
authored
Jun 20, 2020
by
xuxin
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/staging' into staging
parents
60eff80f
3d9a0c0f
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
94 additions
and
52 deletions
+94
-52
BrowserBackendApplication.java
...a/com/edgec/browserbackend/BrowserBackendApplication.java
+10
-0
IpResourceRepositoryCustomImpl.java
...nd/browser/repository/IpResourceRepositoryCustomImpl.java
+1
-1
BrowserTask.java
...va/com/edgec/browserbackend/browser/task/BrowserTask.java
+5
-3
Set3proxyTask.java
.../com/edgec/browserbackend/browser/task/Set3proxyTask.java
+44
-48
TerminateBean.java
.../com/edgec/browserbackend/browser/task/TerminateBean.java
+34
-0
No files found.
src/main/java/com/edgec/browserbackend/BrowserBackendApplication.java
View file @
4d35f9a8
...
...
@@ -9,7 +9,9 @@ import org.springframework.context.annotation.Configuration;
import
org.springframework.core.convert.converter.Converter
;
import
org.springframework.data.convert.Jsr310Converters
;
import
org.springframework.data.mongodb.core.convert.MongoCustomConversions
;
import
org.springframework.scheduling.TaskScheduler
;
import
org.springframework.scheduling.annotation.EnableScheduling
;
import
org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
;
import
org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
;
import
org.springframework.security.oauth2.provider.token.AuthenticationKeyGenerator
;
import
org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator
;
...
...
@@ -49,6 +51,14 @@ public class BrowserBackendApplication {
return
new
CorsFilter
(
source
);
}
@Bean
public
TaskScheduler
taskScheduler
()
{
final
ThreadPoolTaskScheduler
scheduler
=
new
ThreadPoolTaskScheduler
();
scheduler
.
setPoolSize
(
5
);
return
scheduler
;
}
@Configuration
public
class
CORSConfiguration
{
@Bean
...
...
src/main/java/com/edgec/browserbackend/browser/repository/IpResourceRepositoryCustomImpl.java
View file @
4d35f9a8
...
...
@@ -63,7 +63,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
@Override
public
boolean
unLockHealth
(
String
id
)
{
return
fals
e
;
return
tru
e
;
}
@Override
...
...
src/main/java/com/edgec/browserbackend/browser/task/BrowserTask.java
View file @
4d35f9a8
...
...
@@ -348,8 +348,8 @@ public class BrowserTask {
static
OkHttpClient
.
Builder
builder
=
new
OkHttpClient
.
Builder
();
static
{
builder
.
connectTimeout
(
1
0
,
TimeUnit
.
SECONDS
);
builder
.
readTimeout
(
1
0
,
TimeUnit
.
SECONDS
);
builder
.
connectTimeout
(
30
0
,
TimeUnit
.
SECONDS
);
builder
.
readTimeout
(
30
0
,
TimeUnit
.
SECONDS
);
}
static
OkHttpClient
client
=
new
OkHttpClient
(
builder
);
...
...
@@ -377,7 +377,9 @@ public class BrowserTask {
Call
call
=
client
.
newCall
(
request
);
Response
response
=
call
.
execute
();
ResponseBody
responseBody
=
response
.
body
();
return
responseBody
.
string
();
String
rs
=
responseBody
.
string
();
response
.
close
();
return
rs
;
}
}
}
src/main/java/com/edgec/browserbackend/browser/task/Set3proxyTask.java
View file @
4d35f9a8
package
com
.
edgec
.
browserbackend
.
browser
.
task
;
import
com.edgec.browserbackend.account.repository.AccountRepository
;
import
com.edgec.browserbackend.auth.domain.mongo.MongoOAuth2AccessToken
;
import
com.edgec.browserbackend.auth.repository.mongo.MongoOAuth2AccessTokenRepository
;
import
com.edgec.browserbackend.browser.domain.IpResource
;
import
com.edgec.browserbackend.browser.domain.IpType
;
import
com.edgec.browserbackend.browser.domain.ProxyConfig
;
import
com.edgec.browserbackend.browser.repository.IpResourceRepository
;
import
com.edgec.browserbackend.browser.repository.ProxyConfigRepository
;
import
com.edgec.browserbackend.browser.repository.SpecialLineRepository
;
...
...
@@ -19,10 +19,8 @@ import org.springframework.stereotype.Component;
import
java.io.BufferedWriter
;
import
java.io.File
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.time.Instant
;
import
java.util.List
;
import
java.util.stream.Collectors
;
@Component
public
class
Set3proxyTask
{
...
...
@@ -45,20 +43,20 @@ public class Set3proxyTask {
private
ProxyConfigRepository
proxyConfigRepository
;
@Scheduled
(
cron
=
"0 0/2 * * * ?"
)
@SchedulerLock
(
name
=
"proxyTask"
,
lockAtLeastFor
=
60
*
1000
*
5
,
lockAtMostFor
=
60
*
1000
*
9
)
public
void
set3proxy
()
{
long
validTime
=
Instant
.
now
().
minusSeconds
(
43200
).
toEpochMilli
();
List
<
String
>
tokenUsernames
=
mongoOAuth2AccessTokenRepository
.
findByCreatedAtGreaterThan
(
validTime
).
stream
().
map
(
MongoOAuth2AccessToken:
:
getUsername
).
collect
(
Collectors
.
toList
());
List
<
String
>
accountParents
=
accountRepository
.
findByNameIn
(
tokenUsernames
).
stream
().
map
(
x
->
x
.
getParent
()
==
null
?
x
.
getName
()
:
x
.
getParent
()).
distinct
().
collect
(
Collectors
.
toList
());
List
<
IpResource
>
ipResources
=
ipResourceRepository
.
findByOwnerInAndSpecialLine
(
accountParents
,
true
);
// List<IpResource> ipResources = ipResourceRepository.findBySpecialLineAndIsDeleted(true, false);
long
nowtime
=
Instant
.
now
().
toEpochMilli
();
// List<String> tokenUsernames = mongoOAuth2AccessTokenRepository.findByCreatedAtGreaterThan(validTime).stream().map(MongoOAuth2AccessToken::getUsername).collect(Collectors.toList());
// List<String> accountParents = accountRepository.findByNameIn(tokenUsernames).stream().map(x -> x.getParent() == null ? x.getName() : x.getParent()).distinct().collect(Collectors.toList());
// List<IpResource> ipResources = ipResourceRepository.findByOwnerInAndSpecialLine(accountParents, true);
List
<
IpResource
>
ipResources
=
ipResourceRepository
.
findBySpecialLineAndIsDeleted
(
true
,
false
);
File
file
=
new
File
(
"3proxy_"
+
Instant
.
now
().
toEpochMilli
());
try
{
file
.
delete
();
file
.
createNewFile
();
try
{
file
.
delete
();
file
.
createNewFile
();
}
catch
(
Exception
t1
)
{
log
.
error
(
t1
.
getMessage
(),
t1
);
}
FileWriter
fileWriter
=
new
FileWriter
(
file
,
true
);
BufferedWriter
bw
=
new
BufferedWriter
(
fileWriter
);
...
...
@@ -67,55 +65,53 @@ public class Set3proxyTask {
bw
.
write
(
"config /root/3proxy.cfg\n"
);
bw
.
write
(
"monitor /root/3proxy.cfg\n"
);
// 写入 ipResources 相关信息
if
(
ipResources
.
size
()
!=
0
)
{
bwWriteIpResources
(
ipResources
,
bw
);
for
(
IpResource
ipResource
:
ipResources
)
{
if
(
StringUtils
.
isNotBlank
(
ipResource
.
getAddr
()))
bw
.
write
(
"users \""
+
ipResource
.
getProxyUsername
()
+
":CL:"
+
ipResource
.
getProxyPassword
()
+
"\"\n"
);
ipResource
.
setUsingSpecialLine
(
true
);
}
bw
.
write
(
"\nauth strong\n"
);
a:
for
(
IpResource
ipResource
:
ipResources
)
{
if
(
StringUtils
.
isNotBlank
(
ipResource
.
getAddr
()))
{
if
(
ipResource
.
getPort
()
==
null
||
ipResource
.
getPort
().
size
()
==
0
)
continue
a
;
bw
.
write
(
"allow "
+
ipResource
.
getProxyUsername
()
+
"\n"
);
if
(
ipResource
.
getIpType
()
!=
IpType
.
OWN
)
{
bw
.
write
(
"parent 1000 http "
+
ipResource
.
getAddr
()
+
" "
+
(
ipResource
.
getPort
().
size
()
>
1
?
ipResource
.
getPort
().
get
(
1
)
:
ipResource
.
getPort
().
get
(
0
))
+
" fangguanlianbrowser "
+
ipResource
.
getPassword
()
+
"\n"
);
}
else
{
bw
.
write
(
"parent 1000 http "
+
ipResource
.
getAddr
()
+
" "
+
(
ipResource
.
getPort
().
size
()
>
1
?
ipResource
.
getPort
().
get
(
1
)
:
ipResource
.
getPort
().
get
(
0
))
+
" "
+
ipResource
.
getUsername
()
+
" "
+
ipResource
.
getPassword
()
+
"\n"
);
}
}
}
bw
.
write
(
"\nallow none\nproxy -p20004\nsocks -p20005\n"
);
bw
.
flush
();
bw
.
close
();
log
.
info
(
"成功写入文件"
);
log
.
error
(
"成功写入文件"
);
long
nowtime
=
Instant
.
now
().
toEpochMilli
();
long
proxyConfigTimestamp
=
proxyConfigRepository
.
findAll
().
get
(
0
).
getTimestamp
();
if
(
proxyConfigRepository
.
count
()
>
0
&&
nowtime
<
proxyConfigTimestamp
)
{
file
.
delete
();
if
(
proxyConfigRepository
.
count
()
>
0
)
{
ProxyConfig
proxyConfig
=
proxyConfigRepository
.
findAll
().
get
(
0
);
if
(
nowtime
<
proxyConfig
.
getTimestamp
())
{
file
.
delete
();
return
;
}
else
{
proxyConfigRepository
.
updateProxy
(
file
,
nowtime
);
file
.
delete
();
}
}
else
{
proxyConfigRepository
.
updateProxy
(
file
,
nowtime
);
file
.
delete
();
}
}
catch
(
Exception
e
)
{
log
.
error
(
"出错了"
);
log
.
error
(
e
.
getMessage
(),
e
);
}
}
private
void
bwWriteIpResources
(
List
<
IpResource
>
ipResources
,
BufferedWriter
bw
)
throws
IOException
{
for
(
IpResource
ipResource
:
ipResources
)
{
ipResource
.
setUsingSpecialLine
(
true
);
if
(
StringUtils
.
isNotBlank
(
ipResource
.
getAddr
()))
{
bw
.
write
(
"users \""
+
ipResource
.
getProxyUsername
()
+
":CL:"
+
ipResource
.
getProxyPassword
()
+
"\"\n"
);
if
(
ipResource
.
getPort
()
==
null
||
ipResource
.
getPort
().
size
()
==
0
)
{
continue
;
}
bw
.
write
(
"allow "
+
ipResource
.
getProxyUsername
()
+
"\n"
);
String
textStart
=
"parent 1000 http "
+
ipResource
.
getAddr
()
+
" "
;
String
textMiddle
=
ipResource
.
getPort
().
size
()
>
1
?
ipResource
.
getPort
().
get
(
1
)
:
ipResource
.
getPort
().
get
(
0
);
if
(
ipResource
.
getIpType
()
!=
IpType
.
OWN
)
{
bw
.
write
(
textStart
+
textMiddle
+
" fangguanlianbrowser "
+
ipResource
.
getPassword
()
+
"\n"
);
}
else
{
bw
.
write
(
textStart
+
textMiddle
+
" "
+
ipResource
.
getUsername
()
+
" "
+
ipResource
.
getPassword
()
+
"\n"
);
}
}
}
}
}
src/main/java/com/edgec/browserbackend/browser/task/TerminateBean.java
0 → 100644
View file @
4d35f9a8
package
com
.
edgec
.
browserbackend
.
browser
.
task
;
import
com.edgec.browserbackend.account.utils.AccountServicePool
;
import
com.edgec.browserbackend.common.utils.ThreadPoolUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.PreDestroy
;
import
java.util.concurrent.TimeUnit
;
@Component
public
class
TerminateBean
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
TerminateBean
.
class
);
@PreDestroy
public
void
onDestroy
()
throws
Exception
{
AccountServicePool
.
taskPool
.
shutdown
();
ThreadPoolUtils
.
buyIpTasksPool
.
shutdown
();
ThreadPoolUtils
.
queryIpHealth
.
shutdown
();
ThreadPoolUtils
.
queryIpTasksPool
.
shutdown
();
AccountServicePool
.
taskPool
.
awaitTermination
(
5
,
TimeUnit
.
MINUTES
);
ThreadPoolUtils
.
buyIpTasksPool
.
awaitTermination
(
5
,
TimeUnit
.
MINUTES
);
ThreadPoolUtils
.
queryIpHealth
.
awaitTermination
(
5
,
TimeUnit
.
MINUTES
);
ThreadPoolUtils
.
queryIpTasksPool
.
awaitTermination
(
5
,
TimeUnit
.
MINUTES
);
log
.
warn
(
"threadpool is destroyed!!"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment