Skip to content

Commit

Permalink
macOS release
Browse files Browse the repository at this point in the history
  • Loading branch information
wwengg committed Apr 27, 2023
1 parent 58d93a8 commit 3799831
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
21 changes: 15 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"github.com/wwengg/douyin/fay/impl"
"github.com/wwengg/douyin/model"
"github.com/wwengg/douyin/proto"
"github.com/wwengg/douyin/utils"
"io"
Expand All @@ -16,7 +17,7 @@ import (
)

func main() {
configureCA()
utils.ConfigureCA()
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = false

Expand Down Expand Up @@ -71,16 +72,16 @@ func main() {

if nr > 0 {
fullPacket = append(fullPacket, buf[:nr]...)
websocketPacket := newWebsocketPacket(fullPacket)
websocketPacket := model.NewWebsocketPacket(fullPacket)

if !websocketPacket.valid {
if !websocketPacket.Valid {
continue
}

websocketPacket.payload = proxy.FilterWebsocketPacket(websocketPacket.payload, direction, ctx)
encodedPacket := websocketPacket.encode()
websocketPacket.Payload = proxy.FilterWebsocketPacket(websocketPacket.Payload, direction, ctx)
encodedPacket := websocketPacket.Encode()
nw, ew := dst.Write(encodedPacket)
fullPacket = fullPacket[websocketPacket.packetSize:]
fullPacket = fullPacket[websocketPacket.PacketSize:]

if nw < 0 || len(encodedPacket) < nw {
nw = 0
Expand Down Expand Up @@ -125,3 +126,11 @@ func main() {
log.Println("软件准备就绪,请启动【直播伴侣】并且点击【开始直播】")
log.Fatal(http.ListenAndServe(":8001", proxy))
}

type RtmpLive struct {
Data struct {
StreamUrl struct {
RtmpPushUrl string `json:"rtmp_push_url"`
} `json:"stream_url"`
} `json:"data"`
}
2 changes: 1 addition & 1 deletion douyin.go → model/douyin.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package model

type RtmpLive struct {
Data struct {
Expand Down
2 changes: 1 addition & 1 deletion messages.go → model/messages.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package model

import (
"fmt"
Expand Down
46 changes: 23 additions & 23 deletions websocket.go → model/websocket.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package main
package model

import "encoding/binary"

type websocketPacket struct {
valid bool
Valid bool
flags byte
opcode int
opcode_str string
mask bool
payloadLength int
maskingKey []byte
payload []byte
packetSize int
Payload []byte
PacketSize int
}

func newWebsocketPacket(packet []byte) *websocketPacket {
func NewWebsocketPacket(packet []byte) *websocketPacket {
p := &websocketPacket{}
if len(packet) <= 6 {
p.valid = false
p.Valid = false
return p
}
p.valid = true
p.Valid = true
p.flags = packet[0] & 0xF0
p.opcode = int(packet[0] & 0x0F)
p.mask = (packet[1] & 0x80) == 0x80
p.payloadLength = int(packet[1] & 0x7f)
packetStart := 2
if p.payloadLength == 126 {
if len(packet) <= 8 {
p.valid = false
p.Valid = false
return p
}

Expand All @@ -39,7 +39,7 @@ func newWebsocketPacket(packet []byte) *websocketPacket {
}
} else if p.payloadLength == 127 {
if len(packet) <= 14 {
p.valid = false
p.Valid = false
return p
}

Expand All @@ -58,13 +58,13 @@ func newWebsocketPacket(packet []byte) *websocketPacket {
packetStart += 4
}

p.packetSize = packetStart + p.payloadLength
if packetStart > len(packet) || p.packetSize > len(packet) {
p.payload = make([]byte, 0)
p.valid = false
p.PacketSize = packetStart + p.payloadLength
if packetStart > len(packet) || p.PacketSize > len(packet) {
p.Payload = make([]byte, 0)
p.Valid = false
} else {
p.payload = packet[packetStart:p.packetSize]
p.valid = (len(p.payload) == p.payloadLength)
p.Payload = packet[packetStart:p.PacketSize]
p.Valid = (len(p.Payload) == p.payloadLength)
}

if p.opcode == 0x00 {
Expand All @@ -83,21 +83,21 @@ func newWebsocketPacket(packet []byte) *websocketPacket {
p.opcode_str = "Unknown"
}

if p.valid && p.maskingKey != nil {
if p.Valid && p.maskingKey != nil {
for i := 0; i < p.payloadLength; i++ {
p.payload[i] ^= p.maskingKey[i%4]
p.Payload[i] ^= p.maskingKey[i%4]
}
}

return p
}

func (packet *websocketPacket) encode() []byte {
packetLength := 2 + len(packet.maskingKey) + len(packet.payload)
func (packet *websocketPacket) Encode() []byte {
packetLength := 2 + len(packet.maskingKey) + len(packet.Payload)
packetStart := 2 + len(packet.maskingKey)
maskingKeyStart := 2

packet.payloadLength = len(packet.payload)
packet.payloadLength = len(packet.Payload)

if packet.payloadLength > 125 {
packetLength += 2
Expand Down Expand Up @@ -132,12 +132,12 @@ func (packet *websocketPacket) encode() []byte {
// reencode using the masking key
if packet.maskingKey != nil {
copy(buf[maskingKeyStart:], packet.maskingKey)
copy(buf[packetStart:], packet.payload)
for i := 0; i < len(packet.payload); i++ {
copy(buf[packetStart:], packet.Payload)
for i := 0; i < len(packet.Payload); i++ {
buf[i+packetStart] ^= packet.maskingKey[i%4]
}
} else {
copy(buf[packetStart:], packet.payload)
copy(buf[packetStart:], packet.Payload)
}

return buf
Expand Down
4 changes: 2 additions & 2 deletions certificates.go → utils/certificates.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package utils

import (
"crypto/tls"
Expand Down Expand Up @@ -32,7 +32,7 @@ func initializeCA(certificateFile string, privateKeyFile string) (*tls.Certifica
return &ca, nil
}

func configureCA() {
func ConfigureCA() {
var err error
certificateFile := config.Get("CERTIFICATE_FILE", "./certificates/proxy-ca.crt")
privateKeyFile := config.Get("PRIVATE_KEY_FILE", "./certificates/proxy-ca.key")
Expand Down

0 comments on commit 3799831

Please sign in to comment.