[ server] [ func.xray ] - 完成有向向量图的基础功能

This commit is contained in:
zeaslity
2023-02-09 15:56:38 +08:00
parent e54952bc72
commit aff674f985
12 changed files with 328 additions and 7 deletions

View File

@@ -0,0 +1,27 @@
package io.wdd.func.xray.beans.node;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
* class to store edges of the weighted graph
*/
@Data
@NoArgsConstructor
@SuperBuilder(toBuilder = true)
public class Edge {
ProxyNode source, destination;
int weight;
Edge(ProxyNode source, ProxyNode destination, int weight) {
this.source = source;
this.destination = destination;
this.weight = weight;
}
}

View File

@@ -0,0 +1,142 @@
package io.wdd.func.xray.beans.node;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static io.wdd.func.xray.beans.node.ProxyNodeSet.*;
/**
* https://blog.csdn.net/allway2/article/details/115034476
*/
//Graph class
class Graph {
/**
* 网络图的 邻接表
*/
List<List<Integer>> adj_matrix = new ArrayList<>();
public Graph(List<Edge> edgeList) {
// 初始化 邻接表
int totalNode = 0;
for (Edge edge : edgeList) {
totalNode = Math.max(
totalNode,
Math.max(
edge
.getSource()
.getNum(),
edge
.getDestination()
.getNum()
)
);
}
ArrayList<Integer> tmp = new ArrayList<>();
for (int i = 0; i <= totalNode; i++) {
tmp.add(0);
}
for (int i = 0; i <= totalNode; i++) {
adj_matrix.add(new ArrayList<>(tmp));
}
// 连接各个边界条件
for (Edge edge : edgeList) {
int source = edge
.getSource()
.getNum();
int destination = edge
.getDestination()
.getNum();
List<Integer> sourceList = adj_matrix.get(source);
sourceList.add(
destination,
edge.getWeight()
);
}
}
public void printGraphMatrix() {
System.out.println("--------- Graph Matrix ----------");
System.out.println("----------------------------------");
System.out.println();
adj_matrix
.stream()
.forEach(
System.out::println
);
System.out.println("----------------------------------");
}
public void printGraph() {
System.out.println("---------- Graph Adjust ----------");
System.out.println("----------------------------------");
int height = adj_matrix.size()-1 ;
int weight = adj_matrix
.get(0)
.size()-1;
for (int heigh = 0; heigh < height; heigh++) {
List<Integer> list = adj_matrix.get(heigh);
for (int width = 0; width < weight; width++) {
if (list.get(width) != 0) {
System.out.print(ProxyNodeMap.get(list.get(width)) + "\t");
}
}
System.out.print("\n");
}
System.out.println("----------------------------------");
}
public static void main(String[] args) {
List<Edge> edgeList = Arrays.asList(
new Edge(chengdu,
shanghai,
10),
new Edge(shanghai,
seoul2,
40),
new Edge(seoul2,
tokyo2,
10),
new Edge(tokyo2,
phoenix2,
30),
new Edge(tokyo2,
london2,
50),
new Edge(hongkong,
tokyo2,
40),
new Edge(hongkong,
seoul2,
20)
);
Graph graph = new Graph(edgeList);
graph.printGraphMatrix();
System.out.println("=========================");
graph.printGraph();
}
}

View File

@@ -0,0 +1,35 @@
package io.wdd.func.xray.beans.node;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
@Data
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder(toBuilder = true)
public class ProxyNode {
String name;
/**
* 用于定位在图中的位置
*/
int num;
String agentTopicName;
String location;
ProxyNodeType proxyNodeType;
String publicIPv4;
String publicIPv6;
@Override
public String toString() {
return "ProxyNode: [ "+ agentTopicName + " ]";
}
}

View File

@@ -0,0 +1,104 @@
package io.wdd.func.xray.beans.node;
import java.util.HashMap;
public class ProxyNodeSet {
public static final HashMap<Integer, ProxyNode> ProxyNodeMap = new HashMap<>();
public static ProxyNode chengdu;
public static ProxyNode shanghai;
public static ProxyNode hongkong;
public static ProxyNode seoul2;
public static ProxyNode tokyo2;
public static ProxyNode phoenix2;
public static ProxyNode london2;
static {
chengdu = ProxyNode
.builder()
.location("Chengdu")
.num(0)
.publicIPv4("43.136.177.228")
.proxyNodeType(ProxyNodeType.INTERFACE)
.name("chengdu-agent")
.agentTopicName("Chengdu-amd-01")
.build();
shanghai = ProxyNode
.builder()
.location("Shanghai")
.num(1)
.publicIPv4("42.192.52.227")
.proxyNodeType(ProxyNodeType.RELAY)
.name("tencent-shanghai")
.agentTopicName("Shanghai-amd64-01")
.build();
hongkong = ProxyNode
.builder()
.location("Hongkong")
.num(2)
.publicIPv4("43.154.83.213")
.proxyNodeType(ProxyNodeType.EXTERNAL)
.name("tencent-hongkong")
.agentTopicName("Hongkong-amd64-01")
.build();
seoul2 = ProxyNode
.builder()
.location("Seoul")
.num(3)
.publicIPv4("140.238.30.110")
.proxyNodeType(ProxyNodeType.EXTERNAL)
.name("oracle-seoul-2")
.agentTopicName("Seoul-amd64-02")
.build();
tokyo2 = ProxyNode
.builder()
.location("Tokyo")
.num(4)
.publicIPv4("140.238.52.228")
.proxyNodeType(ProxyNodeType.EXTERNAL)
.name("oracle-tokyo-2")
.agentTopicName("Tokyo-amd64-02")
.build();
phoenix2 = ProxyNode
.builder()
.location("Phoenix")
.num(5)
.publicIPv4("129.146.171.163")
.proxyNodeType(ProxyNodeType.EXTERNAL)
.name("oracle-phoenix-2")
.agentTopicName("Phoenix-amd64-02")
.build();
london2 = ProxyNode
.builder()
.location("London")
.num(6)
.publicIPv4("141.147.106.62")
.proxyNodeType(ProxyNodeType.EXTERNAL)
.name("oracle-london-2")
.agentTopicName("London-amd64-02")
.build();
ProxyNodeMap.put(chengdu.getNum(), chengdu);
ProxyNodeMap.put(hongkong.getNum(), hongkong);
ProxyNodeMap.put(shanghai.getNum(), shanghai);
ProxyNodeMap.put(seoul2.getNum(), seoul2);
ProxyNodeMap.put(tokyo2.getNum(), tokyo2);
ProxyNodeMap.put(phoenix2.getNum(), phoenix2);
ProxyNodeMap.put(london2.getNum(), london2);
}
}

View File

@@ -0,0 +1,13 @@
package io.wdd.func.xray.beans.node;
public enum ProxyNodeType {
INTERFACE,
RELAY,
// 关系应该具备包含关系,即下一层的属性包含了上一层的功能
EXTERNAL
}

View File

@@ -1,4 +1,4 @@
package io.wdd.func.xray.beans; package io.wdd.func.xray.beans.xray;
import lombok.Data; import lombok.Data;

View File

@@ -1,4 +1,4 @@
package io.wdd.func.xray.beans; package io.wdd.func.xray.beans.xray;
import lombok.Data; import lombok.Data;

View File

@@ -1,4 +1,4 @@
package io.wdd.func.xray.beans; package io.wdd.func.xray.beans.xray;
import lombok.Data; import lombok.Data;

View File

@@ -1,4 +1,4 @@
package io.wdd.func.xray.beans; package io.wdd.func.xray.beans.xray;
import lombok.Data; import lombok.Data;

View File

@@ -1,4 +1,4 @@
package io.wdd.func.xray.beans; package io.wdd.func.xray.beans.xray;
import io.wdd.func.xray.beans.xray.*; import io.wdd.func.xray.beans.xray.*;

View File

@@ -1,6 +1,6 @@
package io.wdd.func.xray.beans.xray.protocol.inbound.vless; package io.wdd.func.xray.beans.xray.protocol.inbound.vless;
import io.wdd.func.xray.beans.Client; import io.wdd.func.xray.beans.xray.Client;
import lombok.Data; import lombok.Data;
@Data @Data

View File

@@ -1,6 +1,6 @@
package io.wdd.func.xray.beans.xray.protocol.inbound.vmess; package io.wdd.func.xray.beans.xray.protocol.inbound.vmess;
import io.wdd.func.xray.beans.Client; import io.wdd.func.xray.beans.xray.Client;
import lombok.Data; import lombok.Data;
@Data @Data