Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
G
g4proxy
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
g4proxy
Commits
095c04a0
Commit
095c04a0
authored
Apr 16, 2019
by
Administrator
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
端口关闭
parent
2031ac45
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
9 deletions
+30
-9
AvailablePortResManager.java
...om/virjar/g4proxy_new/server/AvailablePortResManager.java
+15
-2
NatServerChannelHandler.java
...om/virjar/g4proxy_new/server/NatServerChannelHandler.java
+1
-0
ClientManager.java
...a/com/virjar/g4proxy_new/server/client/ClientManager.java
+9
-6
NatClientImage.java
.../com/virjar/g4proxy_new/server/client/NatClientImage.java
+5
-1
No files found.
g4proxy-new/src/main/java/com/virjar/g4proxy_new/server/AvailablePortResManager.java
View file @
095c04a0
package
com
.
virjar
.
g4proxy_new
.
server
;
import
com.google.common.base.Splitter
;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Sets
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.BlockingDeque
;
import
java.util.concurrent.LinkedBlockingDeque
;
...
...
@@ -20,6 +22,8 @@ public class AvailablePortResManager {
private
static
final
String
defaultSpaceConfig
=
"20000-25000"
;
private
static
Map
<
String
,
Integer
>
allocatedResources
=
Maps
.
newConcurrentMap
();
public
static
void
init
(
String
spaceConfig
)
{
if
(
inited
.
compareAndSet
(
false
,
true
))
{
...
...
@@ -48,9 +52,18 @@ public class AvailablePortResManager {
portQueue
.
addAll
(
availablePortResource
);
}
public
static
Integer
allocate
()
{
public
synchronized
static
Integer
allocate
(
String
clientId
)
{
if
(
allocatedResources
.
containsKey
(
clientId
))
{
Integer
integer
=
allocatedResources
.
get
(
clientId
);
if
(
integer
!=
null
)
{
return
integer
;
}
}
init
(
defaultSpaceConfig
);
return
portQueue
.
poll
();
Integer
ret
=
portQueue
.
poll
();
allocatedResources
.
put
(
clientId
,
ret
);
return
ret
;
}
}
g4proxy-new/src/main/java/com/virjar/g4proxy_new/server/NatServerChannelHandler.java
View file @
095c04a0
...
...
@@ -171,6 +171,7 @@ public class NatServerChannelHandler extends SimpleChannelInboundHandler<NatMess
ctx
.
close
();
return
;
}
log
.
info
(
"the nat channel InActive ,close "
);
clientManager
.
closeClient
(
clientId
);
super
.
channelInactive
(
ctx
);
}
...
...
g4proxy-new/src/main/java/com/virjar/g4proxy_new/server/client/ClientManager.java
View file @
095c04a0
...
...
@@ -25,7 +25,7 @@ public class ClientManager {
// this.userMappingBootstrap = userMappingBootstrap;
}
public
boolean
registerNewClient
(
String
client
,
Channel
c
hannel
)
{
public
boolean
registerNewClient
(
String
client
,
Channel
natC
hannel
)
{
NatClientImage
natClientImage
=
natClientImageMap
.
get
(
client
);
Integer
mappingPort
;
if
(
natClientImage
!=
null
)
{
...
...
@@ -35,9 +35,9 @@ public class ClientManager {
mappingPort
=
natClientImage
.
getMappingPort
();
//需要关闭所有的连接,但是计数器不能清零。计数器清零可能导致紊乱
natClientImage
.
updateChannel
(
c
hannel
);
natClientImage
.
updateChannel
(
natC
hannel
);
}
else
{
mappingPort
=
AvailablePortResManager
.
allocate
();
mappingPort
=
AvailablePortResManager
.
allocate
(
client
);
if
(
mappingPort
==
null
)
{
log
.
error
(
"failed to allocate port"
);
return
false
;
...
...
@@ -53,10 +53,10 @@ public class ClientManager {
log
.
warn
(
"bind mapping port:{} failed"
,
mappingPort
,
channelFuture
.
cause
());
return
false
;
}
natClientImage
=
new
NatClientImage
(
client
,
mappingPort
,
channel
);
natClientImage
=
new
NatClientImage
(
client
,
mappingPort
,
natChannel
,
channelFuture
.
channel
()
);
}
c
hannel
.
attr
(
Constant
.
NAT_CHANNEL_CLIENT_KEY
).
set
(
client
);
natC
hannel
.
attr
(
Constant
.
NAT_CHANNEL_CLIENT_KEY
).
set
(
client
);
clientPortBiMap
.
put
(
client
,
mappingPort
);
natClientImageMap
.
put
(
client
,
natClientImage
);
log
.
info
(
"client :{} register success,with port:{}"
,
client
,
mappingPort
);
...
...
@@ -77,12 +77,15 @@ public class ClientManager {
public
void
closeClient
(
String
clientId
)
{
log
.
info
(
"close client :{}"
,
clientId
);
NatClientImage
client
=
natClientImageMap
.
remove
(
clientId
);
if
(
client
==
null
)
{
log
.
error
(
"no
w
client registered for clientId:{}"
,
clientId
);
log
.
error
(
"no client registered for clientId:{}"
,
clientId
);
return
;
}
client
.
closeAllUserChannel
();
client
.
getNatChannel
().
close
();
//这样,停止监听对应的代理端口,防止代理请求再次打进来
client
.
getUserMappingServerChannel
().
close
();
}
}
g4proxy-new/src/main/java/com/virjar/g4proxy_new/server/client/NatClientImage.java
View file @
095c04a0
...
...
@@ -20,12 +20,16 @@ public class NatClientImage {
@Getter
private
Channel
natChannel
;
@Getter
private
Channel
userMappingServerChannel
;
private
Map
<
Long
,
Channel
>
userMappingChannels
=
Maps
.
newConcurrentMap
();
public
NatClientImage
(
String
clientId
,
int
mappingPort
,
Channel
natChannel
)
{
public
NatClientImage
(
String
clientId
,
int
mappingPort
,
Channel
natChannel
,
Channel
userMappingServerChannel
)
{
this
.
clientId
=
clientId
;
this
.
mappingPort
=
mappingPort
;
this
.
natChannel
=
natChannel
;
this
.
userMappingServerChannel
=
userMappingServerChannel
;
}
public
void
updateChannel
(
Channel
channel
)
{
...
...
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