Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
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
张蕾
Geth-Modification
Commits
3f503ffc
Commit
3f503ffc
authored
11 years ago
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented support for UPnP
parent
ae0d4eb7
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
180 additions
and
123 deletions
+180
-123
ethereum.go
ethereum.go
+62
-0
nat.go
nat.go
+12
-0
natpmp.go
natpmp.go
+54
-0
natupnp.go
natupnp.go
+52
-123
No files found.
ethereum.go
View file @
3f503ffc
...
...
@@ -8,6 +8,7 @@ import (
"github.com/ethereum/ethwire-go"
"log"
"net"
"strconv"
"sync"
"sync/atomic"
"time"
...
...
@@ -29,6 +30,7 @@ const (
type
Ethereum
struct
{
// Channel for shutting down the ethereum
shutdownChan
chan
bool
quit
chan
bool
// DB interface
//db *ethdb.LDBDatabase
db
*
ethdb
.
MemDatabase
...
...
@@ -48,6 +50,8 @@ type Ethereum struct {
// Capabilities for outgoing peers
serverCaps
Caps
nat
NAT
}
func
New
(
caps
Caps
)
(
*
Ethereum
,
error
)
{
...
...
@@ -57,15 +61,30 @@ func New(caps Caps) (*Ethereum, error) {
return
nil
,
err
}
/*
gateway := net.ParseIP("192.168.192.1")
nat := NewNatPMP(gateway)
port, err := nat.AddPortMapping("tcp", 30303, 30303, "", 60)
log.Println(port, err)
*/
nat
,
err
:=
Discover
()
if
err
!=
nil
{
log
.
Println
(
"UPnP failed"
,
err
)
return
nil
,
err
}
ethutil
.
Config
.
Db
=
db
nonce
,
_
:=
ethutil
.
RandomUint64
()
ethereum
:=
&
Ethereum
{
shutdownChan
:
make
(
chan
bool
),
quit
:
make
(
chan
bool
),
db
:
db
,
peers
:
list
.
New
(),
Nonce
:
nonce
,
serverCaps
:
caps
,
nat
:
nat
,
}
ethereum
.
TxPool
=
ethchain
.
NewTxPool
()
ethereum
.
TxPool
.
Speaker
=
ethereum
...
...
@@ -217,6 +236,8 @@ func (s *Ethereum) Start() {
go
s
.
peerHandler
(
ln
)
}
go
s
.
upnpUpdateThread
()
// Start the reaping processes
go
s
.
ReapDeadPeerHandler
()
...
...
@@ -245,6 +266,8 @@ func (s *Ethereum) Stop() {
p
.
Stop
()
})
close
(
s
.
quit
)
s
.
shutdownChan
<-
true
s
.
TxPool
.
Stop
()
...
...
@@ -254,3 +277,42 @@ func (s *Ethereum) Stop() {
func
(
s
*
Ethereum
)
WaitForShutdown
()
{
<-
s
.
shutdownChan
}
func
(
s
*
Ethereum
)
upnpUpdateThread
()
{
// Go off immediately to prevent code duplication, thereafter we renew
// lease every 15 minutes.
timer
:=
time
.
NewTimer
(
0
*
time
.
Second
)
lport
,
_
:=
strconv
.
ParseInt
(
"30303"
,
10
,
16
)
first
:=
true
out
:
for
{
select
{
case
<-
timer
.
C
:
listenPort
,
err
:=
s
.
nat
.
AddPortMapping
(
"TCP"
,
int
(
lport
),
int
(
lport
),
"eth listen port"
,
20
*
60
)
if
err
!=
nil
{
log
.
Println
(
"can't add UPnP port mapping:"
,
err
)
break
out
}
if
first
&&
err
==
nil
{
externalip
,
err
:=
s
.
nat
.
GetExternalAddress
()
if
err
!=
nil
{
log
.
Println
(
"UPnP can't get external address:"
,
err
)
continue
out
}
log
.
Println
(
"Successfully bound via UPnP to"
,
externalip
,
listenPort
)
first
=
false
}
timer
.
Reset
(
time
.
Minute
*
15
)
case
<-
s
.
quit
:
break
out
}
}
timer
.
Stop
()
if
err
:=
s
.
nat
.
DeletePortMapping
(
"TCP"
,
int
(
lport
),
int
(
lport
));
err
!=
nil
{
log
.
Println
(
"unable to remove UPnP port mapping:"
,
err
)
}
else
{
log
.
Println
(
"succesfully disestablished UPnP port mapping"
)
}
}
This diff is collapsed.
Click to expand it.
nat.go
0 → 100644
View file @
3f503ffc
package
eth
import
(
"net"
)
// protocol is either "udp" or "tcp"
type
NAT
interface
{
GetExternalAddress
()
(
addr
net
.
IP
,
err
error
)
AddPortMapping
(
protocol
string
,
externalPort
,
internalPort
int
,
description
string
,
timeout
int
)
(
mappedExternalPort
int
,
err
error
)
DeletePortMapping
(
protocol
string
,
externalPort
,
internalPort
int
)
(
err
error
)
}
This diff is collapsed.
Click to expand it.
natpmp.go
0 → 100644
View file @
3f503ffc
package
eth
import
(
natpmp
"code.google.com/p/go-nat-pmp"
"fmt"
"net"
)
// Adapt the NAT-PMP protocol to the NAT interface
// TODO:
// + Register for changes to the external address.
// + Re-register port mapping when router reboots.
// + A mechanism for keeping a port mapping registered.
type
natPMPClient
struct
{
client
*
natpmp
.
Client
}
func
NewNatPMP
(
gateway
net
.
IP
)
(
nat
NAT
)
{
return
&
natPMPClient
{
natpmp
.
NewClient
(
gateway
)}
}
func
(
n
*
natPMPClient
)
GetExternalAddress
()
(
addr
net
.
IP
,
err
error
)
{
response
,
err
:=
n
.
client
.
GetExternalAddress
()
if
err
!=
nil
{
return
}
ip
:=
response
.
ExternalIPAddress
addr
=
net
.
IPv4
(
ip
[
0
],
ip
[
1
],
ip
[
2
],
ip
[
3
])
return
}
func
(
n
*
natPMPClient
)
AddPortMapping
(
protocol
string
,
externalPort
,
internalPort
int
,
description
string
,
timeout
int
)
(
mappedExternalPort
int
,
err
error
)
{
if
timeout
<=
0
{
err
=
fmt
.
Errorf
(
"timeout must not be <= 0"
)
return
}
// Note order of port arguments is switched between our AddPortMapping and the client's AddPortMapping.
response
,
err
:=
n
.
client
.
AddPortMapping
(
protocol
,
internalPort
,
externalPort
,
timeout
)
if
err
!=
nil
{
return
}
mappedExternalPort
=
int
(
response
.
MappedExternalPort
)
return
}
func
(
n
*
natPMPClient
)
DeletePortMapping
(
protocol
string
,
externalPort
,
internalPort
int
)
(
err
error
)
{
// To destroy a mapping, send an add-port with
// an internalPort of the internal port to destroy, an external port of zero and a time of zero.
_
,
err
=
n
.
client
.
AddPortMapping
(
protocol
,
internalPort
,
0
,
0
)
return
}
This diff is collapsed.
Click to expand it.
upnp.go
→
nat
upnp.go
View file @
3f503ffc
This diff is collapsed.
Click to expand it.
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