Skip to content

Commit

Permalink
tcp连接队列
Browse files Browse the repository at this point in the history
  • Loading branch information
csznet committed Dec 10, 2023
1 parent 9068471 commit 570ef6a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
21 changes: 3 additions & 18 deletions assets/templates/index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,10 @@
color: #fff;
}

.del {
.but {
display: inline-block; /* 修改为行内块元素 */
width: 32px; /* 设置宽度 */
height: 28px; /* 设置高度 */
background-color: #28a745;
color: white;
border: none;
padding: 8px 12px;
cursor: pointer;
text-decoration-line: none;
text-align: center; /* 居中文本 */
line-height: 30px; /* 设置行高以垂直居中 */
}

.do {
display: inline-block; /* 修改为行内块元素 */
width: 32px; /* 设置宽度 */
height: 28px; /* 设置高度 */
background-color: #d93b3b;
color: white;
border: none;
padding: 8px 12px;
Expand Down Expand Up @@ -124,8 +109,8 @@
<td>{{.Protocol}}</td>
<td>{{if gt .TotalGigabyte 0}}{{.TotalGigabyte}}G{{end}}{{.TotalBytes}}</td>
<td>
<a class="del" href="/del/{{.Id}}">删除</a>
<a class="do" href="/do/{{.Id}}">{{if eq .Status 0}}停用{{else}}启用{{end}}</a>
<a class="but" style="background-color: #28a745;" href="/del/{{.Id}}">删除</a>
<a class="but" style="background-color: #{{if eq .Status 0}}d93b3b{{else}}2181b5{{end}};" href="/do/{{.Id}}">{{if eq .Status 0}}停用{{else}}启用{{end}}</a>
</td>
</tr>
{{end}}
Expand Down
7 changes: 7 additions & 0 deletions forward/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ConnectionStats struct {
conf.ConnectionStats
TotalBytesOld uint64 `gorm:"-"`
TotalBytesLock sync.Mutex `gorm:"-"`
TCPConnections []net.Conn `gorm:"-"` // 新增字段,用于存储 TCP 连接
}

// 保存多个连接信息
Expand Down Expand Up @@ -106,6 +107,10 @@ func Run(stats *ConnectionStats, wg *sync.WaitGroup) {
fmt.Printf("【%s】停止监听端口 %s\n", stats.Protocol, stats.LocalPort)
listener.Close()
cancel()
// 遍历并关闭所有 TCP 连接
for _, conn := range stats.TCPConnections {
conn.Close()
}
return
} else {
conf.Ch <- stopPort
Expand Down Expand Up @@ -144,6 +149,8 @@ func (cs *ConnectionStats) handleTCPConnection(wg *sync.WaitGroup, clientConn ne
}
defer remoteConn.Close()

cs.TCPConnections = append(cs.TCPConnections, clientConn, remoteConn) // 添加连接到列表

var copyWG sync.WaitGroup
copyWG.Add(2)

Expand Down

0 comments on commit 570ef6a

Please sign in to comment.