Skip to content

Commit

Permalink
Fix #3, fix #6: Conntrack module added.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyyf committed Mar 8, 2015
1 parent b687028 commit f0e94be
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 52 deletions.
1 change: 1 addition & 0 deletions .plugins
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github.com/jackyyf/MineGate-Go/plugins/conntrack
github.com/jackyyf/MineGate-Go/plugins/realip
13 changes: 5 additions & 8 deletions build/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ listen: '[::]:25565'
log:
file: minegate.log
level: info
# daemon feature is currently unstable and should not be used during production.
# since golang uses multiple threads and they do not registered fork handler,
# fork process may cause unknown problems.
daemon: true
upstreams:
- hostname: server1.local
Expand All @@ -27,12 +24,12 @@ upstreams:
onerror:
text: 'Fallback is just a joke dude!'

bad_host:
text: 'Invalid host!'
color: red
bold: true

host_not_found:
text: 'No such server served by minegate...'
color: blue
bold: true

conntrack:
brust: 5
interval: 15
limit: 10
7 changes: 0 additions & 7 deletions minegate/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ type Config struct {
Daemonize bool `yaml:"daemon"`
Listen_addr string `yaml:"listen"`
Upstream []*Upstream `yaml:"upstreams"`
BadHost ChatMessage `yaml:"bad_host"`
chatBadHost *mcchat.ChatMsg `yaml:"-"`
NotFound ChatMessage `yaml:"host_not_found"`
chatNotFound *mcchat.ChatMsg `yaml:"-"`
Extras map[string]interface{} `yaml:",inline"`
Expand Down Expand Up @@ -121,15 +119,10 @@ func validateConfig() {
config.Upstream[idx] = nil
config.Upstream = append(config.Upstream[:idx], config.Upstream[idx+1:]...)
}
if config.BadHost.Text == "" {
log.Warn("Empty error text for bad host error, use default string")
config.BadHost.Text = "Bad hostname."
}
if config.NotFound.Text == "" {
log.Warn("Empty error text for not found error, use default string")
config.NotFound.Text = "No such host."
}
config.chatBadHost = ToChatMsg(&config.BadHost)
config.chatNotFound = ToChatMsg(&config.NotFound)
}

Expand Down
22 changes: 11 additions & 11 deletions minegate/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type NetworkEvent struct {
RemoteAddr *net.TCPAddr
connID uintptr
connID uint64
log_prefix string
}

Expand Down Expand Up @@ -70,7 +70,7 @@ func (event *NetworkEvent) GetRemoteIP() (ip string) {
return addr
}

func (event *NetworkEvent) GetConnID() (connID uintptr) {
func (event *NetworkEvent) GetConnID() (connID uint64) {
return event.connID
}

Expand Down Expand Up @@ -322,7 +322,7 @@ func PreLoadConfig() {
if l == nil {
continue
}
log.Infof("Calling PreLoadConfig priority=%d", p)
log.Debugf("Calling PreLoadConfig priority=%d", p)
for _, handler := range l {
handler()
}
Expand All @@ -334,7 +334,7 @@ func PostLoadConfig() {
if l == nil {
continue
}
log.Infof("Calling PostLoadConfig priority=%d", p)
log.Debugf("Calling PostLoadConfig priority=%d", p)
for _, handler := range l {
handler()
}
Expand All @@ -346,7 +346,7 @@ func PostAccept(event *PostAcceptEvent) {
if l == nil {
continue
}
event.Infof("Calling PostAccept priority=%d", p)
event.Debugf("Calling PostAccept priority=%d", p)
for _, handler := range l {
handler(event)
}
Expand All @@ -358,7 +358,7 @@ func PreRouting(event *PreRoutingEvent) {
if l == nil {
continue
}
event.Infof("Calling PreRouting priority=%d", p)
event.Debugf("Calling PreRouting priority=%d", p)
for _, handler := range l {
handler(event)
}
Expand All @@ -370,7 +370,7 @@ func PingRequest(event *PingRequestEvent) {
if l == nil {
continue
}
event.Infof("Calling PingRequest priority=%d", p)
event.Debugf("Calling PingRequest priority=%d", p)
for _, handler := range l {
handler(event)
}
Expand All @@ -382,7 +382,7 @@ func PreStatusResponse(event *PreStatusResponseEvent) {
if l == nil {
continue
}
event.Infof("Calling PreStatusResponse priority=%d", p)
event.Debugf("Calling PreStatusResponse priority=%d", p)
for _, handler := range l {
handler(event)
}
Expand All @@ -394,7 +394,7 @@ func StartProxy(event *StartProxyEvent) {
if l == nil {
continue
}
event.Infof("Calling StartProxy priority=%d", p)
event.Debugf("Calling StartProxy priority=%d", p)
for _, handler := range l {
handler(event)
}
Expand All @@ -406,7 +406,7 @@ func LoginRequest(event *LoginRequestEvent) {
if l == nil {
continue
}
event.Infof("Calling PingRequest priority=%d", p)
event.Debugf("Calling PingRequest priority=%d", p)
for _, handler := range l {
handler(event)
}
Expand All @@ -418,7 +418,7 @@ func Disconnect(event *DisconnectEvent) {
if l == nil {
continue
}
event.Infof("Calling Disconnect priority=%d", p)
event.Debugf("Calling Disconnect priority=%d", p)
for _, handler := range l {
handler(event)
}
Expand Down
2 changes: 2 additions & 0 deletions minegate/minegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
)

func Run() {
PreLoadConfig()
confInit()
PostLoadConfig()
log.Infof("MineGate %s started.", version_full)
go ServerSocket()
sig := make(chan os.Signal, 1)
Expand Down
8 changes: 2 additions & 6 deletions minegate/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type Upstream struct {
Extras map[string]interface{} `yaml:",inline"`
}

var valid_host = []byte("0123456789abcdefgijklmnopqrstuvwxyz.-")
var valid_pattern = []byte("0123456789abcdefgijklmnopqrstuvwxyz.-*?")
var valid_host = []byte("0123456789abcdefgijklmnopqrstuvwxyz.-:[]")
var valid_pattern = []byte("0123456789abcdefgijklmnopqrstuvwxyz.-:*?[]")

func (upstream *Upstream) Validate() (valid bool) {
var host, port string
Expand Down Expand Up @@ -134,10 +134,6 @@ func GetUpstream(hostname string) (upstream *Upstream, err *mcchat.ChatMsg) {
config_lock.Lock()
defer config_lock.Unlock()
log.Debugf("hostname=%s", hostname)
hostname = strings.ToLower(hostname)
if !CheckHost(hostname) {
return nil, config.chatBadHost
}
for _, u := range config.Upstream {
log.Debugf("pattern=%s", u.Pattern)
if matched, _ := path.Match(u.Pattern, hostname); matched {
Expand Down
13 changes: 7 additions & 6 deletions minegate/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ type ReaderWriter interface {

type WrapedSocket struct {
sock net.Conn
id uintptr
id uint64
log_prefix string
client bool
*bufio.Reader
}

var counter uintptr = 0
var counter uint64 = 0

func WrapUpstreamSocket(conn net.Conn, cws *WrapedSocket) (ws *WrapedSocket) {
ws = new(WrapedSocket)
ws.sock = conn
ws.Reader = bufio.NewReader(conn)
ws.id = cws.Id()
ws.log_prefix = fmt.Sprintf("[#%d %s]", ws.id, conn.RemoteAddr())
ws.log_prefix = fmt.Sprintf("[#%d %s] ", ws.id, conn.RemoteAddr())
ws.client = false
return
}
Expand All @@ -42,12 +42,12 @@ func WrapClientSocket(conn net.Conn) (ws *WrapedSocket) {
ws.Reader = bufio.NewReader(conn)
ws.id = counter
counter++
ws.log_prefix = fmt.Sprintf("[#%d %s]", ws.id, conn.RemoteAddr())
ws.log_prefix = fmt.Sprintf("[#%d %s] ", ws.id, conn.RemoteAddr())
ws.client = true
return
}

func (ws *WrapedSocket) Id() uintptr {
func (ws *WrapedSocket) Id() uint64 {
return ws.id
}

Expand All @@ -56,7 +56,8 @@ func (ws *WrapedSocket) Write(b []byte) (n int, err error) {
}

func (ws *WrapedSocket) Close() error {
if ws.client {
if !ws.client {
// Disconnect from upstream.
de := new(DisconnectEvent)
de.connID = ws.id
de.RemoteAddr = ws.sock.RemoteAddr().(*net.TCPAddr)
Expand Down
2 changes: 1 addition & 1 deletion plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
package main
import (
{import_packages}
{import_packages}
)
"""

Expand Down
Loading

0 comments on commit f0e94be

Please sign in to comment.