1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use std::path::PathBuf;
use opcua_core::config::Config;
use crate::{
constants,
config::{ServerConfig, ServerEndpoint, ServerUserToken, ANONYMOUS_USER_TOKEN_ID},
server::Server,
};
const DEFAULT_ENDPOINT_PATH: &str = "/";
pub struct ServerBuilder {
config: ServerConfig,
}
impl ServerBuilder {
pub fn new() -> Self {
Self {
config: ServerConfig::default()
}
}
pub fn from_config(config: ServerConfig) -> Self {
Self { config }
}
pub fn new_anonymous<T>(application_name: T) -> Self where T: Into<String> {
let user_token_ids = vec![ANONYMOUS_USER_TOKEN_ID.to_string()];
Self::new()
.application_name(application_name)
.endpoint("none", ServerEndpoint::new_none(DEFAULT_ENDPOINT_PATH, &user_token_ids))
.discovery_urls(vec![
DEFAULT_ENDPOINT_PATH.into()
])
}
pub fn new_sample() -> Self {
warn!("Sample configuration is for testing purposes only. Use a proper configuration in your production environment");
let path = DEFAULT_ENDPOINT_PATH;
let user_token_ids = ["sample_password_user", "sample_x509_user", ANONYMOUS_USER_TOKEN_ID]
.iter().map(|u| u.to_string()).collect::<Vec<String>>();
Self::new()
.application_name("OPC UA Sample Server")
.application_uri("urn:OPC UA Sample Server")
.product_uri("urn:OPC UA Sample Server Testkit")
.create_sample_keypair(true)
.discovery_server_url(Some(constants::DEFAULT_DISCOVERY_SERVER_URL.to_string()))
.user_token("sample_password_user", ServerUserToken {
user: "sample1".to_string(),
pass: Some("sample1pwd".to_string()),
x509: None,
thumbprint: None,
})
.user_token("sample_x509_user", ServerUserToken {
user: "sample_x509".to_string(),
pass: None,
x509: Some("./users/sample-x509.der".to_string()),
thumbprint: None,
})
.user_token("unused_user", ServerUserToken {
user: "unused".to_string(),
pass: Some("unused1".to_string()),
x509: None,
thumbprint: None,
})
.endpoints(vec![
("none", ServerEndpoint::new_none(path, &user_token_ids)),
("basic128rsa15_sign", ServerEndpoint::new_basic128rsa15_sign(path, &user_token_ids)),
("basic128rsa15_sign_encrypt", ServerEndpoint::new_basic128rsa15_sign_encrypt(path, &user_token_ids)),
("aes128-sha256-rsaoaep_sign", ServerEndpoint::new_aes128_sha256_rsaoaep_sign(path, &user_token_ids)),
("aes128-sha256-rsaoaep_sign_encrypt", ServerEndpoint::new_aes128_sha256_rsaoaep_sign_encrypt(path, &user_token_ids)),
("aes256-sha256-rsapss_sign", ServerEndpoint::new_aes256_sha256_rsapss_sign(path, &user_token_ids)),
("aes256-sha256-rsapss_sign_encrypt", ServerEndpoint::new_aes256_sha256_rsapss_sign_encrypt(path, &user_token_ids)),
("basic256_sign", ServerEndpoint::new_basic256_sign(path, &user_token_ids)),
("basic256_sign_encrypt", ServerEndpoint::new_basic256_sign_encrypt(path, &user_token_ids)),
("basic256sha256_sign", ServerEndpoint::new_basic256sha256_sign(path, &user_token_ids)),
("basic256sha256_sign_encrypt", ServerEndpoint::new_basic256sha256_sign_encrypt(path, &user_token_ids)),
("no_access", ServerEndpoint::new_none("/noaccess", &[]))
])
.discovery_urls(vec![
DEFAULT_ENDPOINT_PATH.into()
])
}
pub fn server(self) -> Option<Server> {
if self.is_valid() {
Some(Server::new(self.config()))
} else {
None
}
}
pub fn config(self) -> ServerConfig {
self.config
}
pub fn is_valid(&self) -> bool {
self.config.is_valid()
}
pub fn application_name<T>(mut self, application_name: T) -> Self where T: Into<String> {
self.config.application_name = application_name.into();
self
}
pub fn application_uri<T>(mut self, application_uri: T) -> Self where T: Into<String> {
self.config.application_uri = application_uri.into();
self
}
pub fn product_uri<T>(mut self, product_uri: T) -> Self where T: Into<String> {
self.config.product_uri = product_uri.into();
self
}
pub fn create_sample_keypair(mut self, create_sample_keypair: bool) -> Self {
self.config.create_sample_keypair = create_sample_keypair;
self
}
pub fn pki_dir<T>(mut self, pki_dir: T) -> Self where T: Into<PathBuf> {
self.config.pki_dir = pki_dir.into();
self
}
pub fn endpoint<T>(mut self, endpoint_id: T, endpoint: ServerEndpoint) -> Self where T: Into<String> {
self.config.endpoints.insert(endpoint_id.into(), endpoint);
self
}
pub fn endpoints<T>(mut self, endpoints: Vec<(T, ServerEndpoint)>) -> Self where T: Into<String> {
for e in endpoints {
self.config.endpoints.insert(e.0.into(), e.1);
};
self
}
pub fn user_token<T>(mut self, user_token_id: T, user_token: ServerUserToken) -> Self where T: Into<String> {
self.config.user_tokens.insert(user_token_id.into(), user_token);
self
}
pub fn discovery_server_url(mut self, discovery_server_url: Option<String>) -> Self {
self.config.discovery_server_url = discovery_server_url;
self
}
pub fn host_and_port<T>(mut self, host: T, port: u16) -> Self where T: Into<String> {
self.config.tcp_config.host = host.into();
self.config.tcp_config.port = port;
self
}
pub fn discovery_urls(mut self, discovery_urls: Vec<String>) -> Self {
self.config.discovery_urls = discovery_urls.iter().map(|discovery_url| {
if discovery_url.starts_with("/") {
format!("opc.tcp://{}:{}/", self.config.tcp_config.host, self.config.tcp_config.port)
} else {
discovery_url.clone()
}
}).collect();
self
}
pub fn max_subscriptions(mut self, max_subscriptions: u32) -> Self {
self.config.limits.max_subscriptions = max_subscriptions;
self
}
pub fn max_monitored_items_per_sub(mut self, max_monitored_items_per_sub: u32) -> Self {
self.config.limits.max_monitored_items_per_sub = max_monitored_items_per_sub;
self
}
pub fn max_array_length(mut self, max_array_length: u32) -> Self {
self.config.limits.max_array_length = max_array_length;
self
}
pub fn max_string_length(mut self, max_string_length: u32) -> Self {
self.config.limits.max_string_length = max_string_length;
self
}
pub fn max_byte_string_length(mut self, max_byte_string_length: u32) -> Self {
self.config.limits.max_byte_string_length = max_byte_string_length;
self
}
pub fn trust_client_certs(mut self) -> Self {
self.config.trust_client_certs = true;
self
}
pub fn clients_can_modify_address_space(mut self) -> Self {
self.config.limits.clients_can_modify_address_space = true;
self
}
}