Skip to content

Commit

Permalink
add support of dialer for TCP
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandrkorzhenevskyi committed Jul 9, 2024
1 parent 855dbc7 commit e361267
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 6 additions & 0 deletions v8/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/jcmturner/gokrb5/v8/krberror"
"github.com/jcmturner/gokrb5/v8/messages"
"github.com/jcmturner/gokrb5/v8/types"
"golang.org/x/net/proxy"
)

// Client side configuration and state.
Expand All @@ -28,6 +29,7 @@ type Client struct {
settings *Settings
sessions *sessions
cache *Cache
tcpDialer *proxy.Dialer
}

// NewWithPassword creates a new client from a password credential.
Expand Down Expand Up @@ -327,3 +329,7 @@ func (cl *Client) Print(w io.Writer) {
k, _ := cl.Credentials.Keytab().JSON()
fmt.Fprintf(w, "Keytab:\n%s\n", k)
}

func (cl *Client) SetDialerForTCP(dialer *proxy.Dialer) {
cl.tcpDialer = dialer
}
17 changes: 13 additions & 4 deletions v8/client/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/jcmturner/gokrb5/v8/iana/errorcode"
"github.com/jcmturner/gokrb5/v8/messages"
"golang.org/x/net/proxy"
)

// SendToKDC performs network actions to send data to the KDC.
Expand Down Expand Up @@ -132,18 +133,26 @@ func (cl *Client) sendKDCTCP(realm string, b []byte) ([]byte, error) {
if err != nil {
return r, err
}
r, err = dialSendTCP(kdcs, b)
r, err = dialSendTCP(kdcs, b, cl.tcpDialer)
if err != nil {
return r, err
}
return checkForKRBError(r)
}

// dialKDCTCP establishes a TCP connection to a KDC.
func dialSendTCP(kdcs map[int]string, b []byte) ([]byte, error) {
func dialSendTCP(kdcs map[int]string, b []byte, dialer *proxy.Dialer) ([]byte, error) {
var errs []string
for i := 1; i <= len(kdcs); i++ {
conn, err := net.DialTimeout("tcp", kdcs[i], 5*time.Second)
var conn net.Conn
var err error

if dialer != nil {
conn, err = dialer.Dial("tcp", kdcs[i])
} else {
conn, err = net.DialTimeout("tcp", kdcs[i], 5*time.Second)
}

if err != nil {
errs = append(errs, fmt.Sprintf("error establishing connection to %s: %v", kdcs[i], err))
continue
Expand All @@ -155,7 +164,7 @@ func dialSendTCP(kdcs map[int]string, b []byte) ([]byte, error) {
// conn is guaranteed to be a TCPConn
rb, err := sendTCP(conn.(*net.TCPConn), b)
if err != nil {
errs = append(errs, fmt.Sprintf("error sneding to %s: %v", kdcs[i], err))
errs = append(errs, fmt.Sprintf("error sending to %s: %v", kdcs[i], err))
continue
}
return rb, nil
Expand Down
2 changes: 1 addition & 1 deletion v8/client/passwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (cl *Client) sendToKPasswd(msg kadmin.Request) (r kadmin.Reply, err error)
return
}
} else {
rb, err = dialSendTCP(kps, b)
rb, err = dialSendTCP(kps, b, cl.tcpDialer)
if err != nil {
return
}
Expand Down

0 comments on commit e361267

Please sign in to comment.